我有相同的过滤器,但在两个不同的地方,一个是作用域应用程序并在模块中定义,一个是作用域 Controller 。
现在,当对相同的 ng-repeat 显示使用相同的过滤器时,它实际上会输出两个不同的参数。 应用程序范围内的过滤器将所有对象作为过滤器中的参数,而 Controller 范围内的过滤器一次只接受每个项目,这是为什么?
代码示例
本地 Controller 作用域过滤器
$scope.localFilter = function(item){
console.log(item); //Single item
return true;
};
//Called like so:
<div ng-repeat="post in currentStation.posts | filter:$parent.localFilter">
应用范围过滤器
angular.module('customFilters', [])
//Used for filtering items from the data usually in an ng-repeat.
.filter('globalFilter', function() {
return function(items)
{
console.log(items); //multiple items
};
});
//Called like so:
<div ng-repeat="post in currentStation.posts | globalFilter">
最佳答案
这里
"post in $parent.currentStation.posts | filter:$parent.filterSearchText">
您正在使用 filterSearchText 作为谓词函数(filterFilter 的参数)
这里...
<div ng-repeat="post in $parent.currentStation.posts | filterSearchText">
您正在使用 filterSearchText 作为自定义过滤器。
http://ngtutorial.com/learn/filter.html
更新
你写过滤器的方式是这样的......
expression | filter_name:argument1:argument2:...
你甚至可以像这样链接它......
expression | filter_name:argument1:... | filter_name:argument1:... | ...
在第一个示例中,过滤器名称是“filter”
在第二个示例中,过滤器名称是“filterSearchText”
... 所以第一个示例是使用 filterSearchText 作为“过滤器”过滤器的参数。 “globalFilter”用作过滤器。
您真正要问的是为什么“过滤器”过滤器会像这样处理第一个参数……答案是 AngularJS 团队以这种方式设计。
关于javascript - AngularJS ng-repeat 过滤器根据范围有两个不同的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25800710/