AngularJS Filter:返回与预期字符串不匹配的对象数组

时间:2015-08-29 10:47:17

标签: javascript angularjs angularjs-filter

在AngularJS中,'filter' filter可以查看对象数组,并返回仅包含字符串属性值的数组,该数组包含字符串比较器。

以下将给出对象数组people的每个成员的列表,其中包含surname: 'Smith'occupation: 'Blacksmith'address_1: '123 Smithfield Lane'等任何属性。

<ul>
    <li ng-repeat="person in people | filter: 'Smith'">
        {{ person.name }}
    </li>
</ul>

使用AngularJS获取包含字符串'Smith'的对象数组的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

来自https://docs.angularjs.org/api/ng/filter/filter

  

用于从数组中选择项目的谓词。

     

可以是以下之一:

     

string:该字符串用于匹配数组的内容。将返回与此字符串匹配的数组中具有字符串属性的所有字符串或对象。这也适用于嵌套对象属性。可以通过在字符串前添加!

来否定谓词
<li ng-repeat="person in people | filter: '!Smith'">
    {{ person.name }}
</li>

如果要按名称过滤,请仅使用对象谓词

<li ng-repeat="person in people | filter: { name : '!Smith'}">
    {{ person.name }}
</li>
相关问题