在角度手册中过滤选项的例子中,`search`是什么?

时间:2014-07-18 06:01:20

标签: angularjs

给出的示例中searchsearch.$search.name中的search.phone是什么<table id="searchTextResults"> <tr><th>Name</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>Name</th><th>Phone</th></tr> <tr ng-repeat="friendObj in friends | filter:search:strict"> <td>{{friendObj.name}}</td> <td>{{friendObj.phone}}</td> </tr> </table>

{{1}}

2 个答案:

答案 0 :(得分:1)

我自己已经弄明白了。表达式ng-model="search.$"ng-model="search.name"ng-model="search.phone"在范围内使用属性$namephone创建对象:

$scope.search = {
    "$":"",
    "name":"",
    "phone":""
}

然后,该文档解释了这里的搜索表达式{{ filter_expression | filter : expression : comparator}}可以有一个对象的形式:

Object: A pattern object can be used to filter specific properties on objects contained by array. For example {name:"M", phone:"1"} predicate will return an array of items which have property name containing "M" and property phone containing "1". A special property name $ can be used (as in {$:"text"}) to accept a match against any property of the object. That's equivalent to the simple substring match with a string as described above.

所以search这里是由ng-model创建的范围上的对象,它具有绑定到输入元素的属性。

答案 1 :(得分:0)

此处search是您在下一行中作为expression传递的对象。

<tr ng-repeat="friendObj in friends | filter:search:strict">

所以在ng-model中你说的是search.$search.name&amp; search.phone

我们假设您将a放入输入search.$,用filter:search:strict替换filter:{$:'a'}:strict

当您将a放入输入search.name时,它会将filter:search:strict替换为filter:{name:'a'}:strict

当您将a放入输入search.phone时,它会将filter:search:strict替换为filter:{phone:'a'}:strict