如何在Angular中过滤嵌套对象不相等?

时间:2014-11-04 18:56:23

标签: javascript angularjs

可能会遗漏一些简单的语法,但我似乎无法使用相同的过滤器来工作:

我能做到

filter: {property:{text:'yes'}}

但不是

filter: {property:{text:'!yes'}}

适用于非嵌套对象。

HTML:

<ul>
  <li ng-repeat="attr in attributes | filter: {property:{text:'!yes'}}">
    {{attr.property.text}}
  </li>
</ul>

JS:

$scope.attributes = [
  {property: { text:'yes' }},
  {property: { text:'no' }},
];

Plunkr链接:

http://plnkr.co/edit/2mTcQijmfnqAM5vUtKsK?p=preview

2 个答案:

答案 0 :(得分:6)

您可以使用ng-if获得与此类似的效果:

<ul>
    <li ng-repeat="attr in attributes" ng-if="attr.property.text !== 'yes'">
        {{attr.property.text}}
    </li>
</ul>

或者,您可以编写包含逻辑的自定义过滤器或以某种方式展平原始结构。我不认为嵌套结构中支持!

答案 1 :(得分:4)

您可以使用

filter: !{property:{text:'yes'}}

这在您无法使用ng-if之类

的情况下非常有用
<select ng-options="element in elementList | filter: !{property: {text:'yes'} }" />

(我知道你可以在选项中使用ng-repeat,但你明白我的意思......)