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

标签 javascript angularjs angularjs-filter

在 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/

相关文章:

javascript - 需要 Angular Material md 复选框

angularjs - 如何使用 AngularJS 对结果进行分页但在完整列表中进行过滤?

javascript - Angularjs从文本框中搜索常量值+值

javascript - 我无法让连续的 Canvas 矩形在 js 中具有重复的线性渐变。任何人都知道如何?

javascript - 如何将设置对象从 Controller 传递给服务?

javascript - 从外部事件控制angularjs ui切换

javascript - 带有自定义过滤器的 Angularjs ng-bind-html

javascript - 当页面中发生有趣的事情时如何照亮浏览器窗口/选项卡?

javascript - Ember : "import Route from ' @ember/routing/route';"throws error in Console

javascript - 以两种方式计算包含和不含增值税的价格,保持相同的结果