©
This document uses PHP Chinese website manual Release
从array
中选择一个条目子集,并作为一个新数组返回。
{{ filter_expression | filter : expression : comparator}}
$filter('filter')(array, expression, comparator)
参数 | 类型 | 详述 |
---|---|---|
array | Array | 源数组。 |
expression | stringObjectfunction() |
从 可以为以下之一:
|
comparator | function(actual, expected)trueundefined |
用于检测期望值(来自过滤表达式)和实际值(来自数组中的实际对象)是否匹配的比较器。 可以为以下之一:
|
<div ng-init="friends = [{name:'John', phone:'555-1276'},
{name:'Mary', phone:'800-BIG-MARY'},
{name:'Mike', phone:'555-4321'},
{name:'Adam', phone:'555-5678'},
{name:'Julie', phone:'555-8765'},
{name:'Juliette', phone:'555-5678'}]"></div>
Search: <input ng-model="searchText">
<table id="searchTextResults">
<tr><th>名称</th><th>Phone</th></tr>
<tr ng-repeat="friend in friends | filter:searchText">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
</tr>
</table><hr>
Any: <input ng-model="search.$"> <br>
Name only <input ng-model="search.name"><br>
Phone only <input ng-model="search.phone"><br>
Equality <input Type="checkbox" ng-model="strict"><br>
<table id="searchObjResults">
<tr><th>名称</th><th>Phone</th></tr>
<tr ng-repeat="friendObj in friends | filter:search:strict">
<td>{{friendObj.name}}</td>
<td>{{friendObj.phone}}</td>
</tr>
</table>
var expectFriendNames = Function(expectedNames, key) {
element.all(by.repeater(key + ' in friends').column(key + '.name')).then(Function(arr) {
arr.forEach(Function(wd, i) {
expect(wd.getText()).toMatch(expectedNames[i]);
});
});};
it('should search across all fields when filtering with a string', Function() {
var searchText = element(by.model('searchText'));
searchText.clear();
searchText.sendKeys('m');
expectFriendNames(['Mary', 'Mike', 'Adam'], 'friend');
searchText.clear();
searchText.sendKeys('76');
expectFriendNames(['John', 'Julie'], 'friend');});
it('should search in specific fields when filtering with a predicate object', Function() {
var searchAny = element(by.model('search.$'));
searchAny.clear();
searchAny.sendKeys('i');
expectFriendNames(['Mary', 'Mike', 'Julie', 'Juliette'], 'friendObj');});
it('should use a equal comparison when comparator is true', Function() {
var searchName = element(by.model('search.name'));
var strict = element(by.model('strict'));
searchName.clear();
searchName.sendKeys('Julie');
strict.click();
expectFriendNames(['Julie'], 'friendObj');});