gpt4 book ai didi

javascript - AngularJS过滤器: Return array of objects not matching expected string

转载 作者:行者123 更新时间:2023-12-02 15:44:55 27 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

The predicate to be used for selecting items from array.

Can be one of:

string: The string is used for matching against the contents of the array. All strings or objects with string properties in array that match this string will be returned. This also applies to nested object properties. The predicate can be negated by prefixing the string with !

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

如果您想按名称过滤,则仅使用对象谓词

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

关于javascript - AngularJS过滤器: Return array of objects not matching expected string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com