javascript ngRepeat 对象上的高级 AngularJS 自定义过滤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17118180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Advanced AngularJS custom filtering on ngRepeat objects
提问by acconrad
I want to achieve the following theoretical code:
我想实现以下理论代码:
VIEW.html
查看.html
<li ng-repeat="player in players | filter:myCustomFilter(player)">{{player.name}}
CONTROLLER.js
控制器.js
// some theoretical conditional statement that return a boolean
$scope.otherCondition = true;
$scope.myCustomFilter = function(player) {
return player.name.substring(0,1).match(/A/gi) && $scope.otherCondition;
}
So I want all of my players to be loaded into an Angular model, but I only want to render players into the DOM whose names start with the letter 'A'. When I try and do something like this, my console informs me that player
is undefined. Do I need to write a custom filter in order to achieve this (via angular.module().filter()
)?
所以我希望我的所有玩家都被加载到一个 Angular 模型中,但我只想将玩家渲染到名称以字母“A”开头的 DOM 中。当我尝试做这样的事情时,我的控制台通知我player
未定义。我是否需要编写自定义过滤器才能实现此目的(通过angular.module().filter()
)?
回答by orlenko
Here's a working fiddle: http://jsfiddle.net/orlenko/jV6DK/
这是一个工作小提琴:http: //jsfiddle.net/orlenko/jV6DK/
Html code (exactly as Karl Zilles suggested):
Html 代码(正如 Karl Zilles 建议的那样):
<div ng-app>
<div ng-controller="MyController">
<h2>Names starting with "A":</h2>
<ul>
<li ng-repeat="player in players | filter:myCustomFilter">{{player.name}}</li>
</ul>
<h2>All Names:</h2>
<ul>
<li ng-repeat="player in players">{{player.name}}</li>
</ul>
</div>
</div>
Javascript:
Javascript:
function MyController($scope) {
$scope.players = [{
name: 'Arthur'
}, {
name: 'William'
}, {
name: 'Bertha'
}, {
name: 'Alice'
}];
$scope.otherCondition = true;
$scope.myCustomFilter = function(player) {
return player.name.substring(0,1).match(/A/gi) && $scope.otherCondition;
}
}
回答by Karen Zilles
You don't need to pass player to the filter
您不需要将播放器传递给过滤器
<li ng-repeat="player in players | filter:myCustomFilter">{{player.name}}
Should work
应该管用
回答by Amir Mog
The answers given are only partially correct, if you need to pass more arguments to the function you would need to create a closure and pass those arguments to the closure as follow:
给出的答案只是部分正确,如果您需要向函数传递更多参数,则需要创建一个闭包并将这些参数传递给闭包,如下所示:
The 'A'
is passed to the closure and player
is passed as a part of the context.
将'A'
被传递到闭合和player
作为上下文的一部分被传递。
HTML:
HTML:
<div ng-app>
<div ng-controller="MyController">
<h2>Names starting with "A":</h2>
<ul>
<li ng-repeat="player in players | filter:myCustomFilter('A')">{{player.name}}</li>
</ul>
<h2>All Names:</h2>
<ul>
<li ng-repeat="player in players">{{player.name}}</li>
</ul>
</div>
</div>
JS:
JS:
function MyController($scope) {
$scope.players = [{
name: 'Arthur'
}, {
name: 'William'
}, {
name: 'Bertha'
}, {
name: 'Alice'
}];
$scope.otherCondition = true;
$scope.myCustomFilter = function(letter) {
return function(player) {
var rgxp = new RegExp(letter, "g");
return player.name.substring(0, 1).match(rgxp) && $scope.otherCondition;
}
}
}