自定义过滤器';过滤器';在Angularjs

自定义过滤器';过滤器';在Angularjs,angularjs,angularjs-filter,Angularjs,Angularjs Filter,我有一个json数组,其中包含月数和年数。我在模板中迭代该数组并显示月份和年份。我正在使用自定义筛选器“monthName”将月份编号转换为月份名称。现在,我想根据用户输入过滤掉显示的月份名称和年份列表。无法对数组使用angular js内置筛选器,因为它包含月号而不是名称 下面是代码片段 JS HTML 因此,当用户输入“三月”或“2016”或“三月(2016)”时,可能与“三月(2016)”匹配。因此,在Angularjs中是否可以构建自定义筛选器或调用筛选器上的函数以获得预期结果?如果符合

我有一个json数组,其中包含月数和年数。我在模板中迭代该数组并显示月份和年份。我正在使用自定义筛选器“monthName”将月份编号转换为月份名称。现在,我想根据用户输入过滤掉显示的月份名称和年份列表。无法对数组使用angular js内置筛选器,因为它包含月号而不是名称

下面是代码片段

JS

HTML


因此,当用户输入“三月”或“2016”或“三月(2016)”时,可能与“三月(2016)”匹配。因此,在Angularjs中是否可以构建自定义筛选器或调用筛选器上的函数以获得预期结果?

如果符合您的条件,您应该使用正则表达式测试数组中的每个元素,下面是表达式
/(\[)?March(2016)?.*(\])?[0-9]{4}/
要在web控制台中选中复制并粘贴此代码以查看结果:
/(\[)?2016年3月?.*(\])?[0-9]{4}/.test('(2016年3月)
并更改
test()
参数。
根据test()返回的布尔值,将在已定义的同一控制器中过滤数组

$scope.monthYr,注入angularJs
$filter
,并执行以下操作

$scope.filterMounthYr=function(){
//$scope.search是输入值
$scope.monthYr=$filter($filter')($scope.monthYr,$scope.search);

}
数组$scope.monthYr确实包含月份编号,而不是前面所述的名称。那么,$filter如何根据用户的输入过滤掉这个数组呢?输入是月名还是年,或者两者都是?谢谢@mahan。但是有没有过滤用户输入击键上的项目的方法,比如我们在本例中使用的角度过滤器html?
$scope.monthYr = [{month:"1", year: "2014"},{month:"2", year: "2015"},{month:"3", year: "2016"}

//custom filter to convert month number to month names
    app.filter('monthName', [function() {
        return function (monthNumber) { 
            var monthNames = [ 'January', 'February', 'March','April', 'May', 'June','July', 'August', 'September','October','November', 'December' ];
        return monthNames[monthNumber - 1];
    }
<div ng-repeat="item in monthYr ">
{{item.month | monthName}} &nbsp;({{item.year}})
</div>
January (2014)
February (2015)
March (2016)