2014-11-04 131 views
5

可能会丢失一些简单的语法,但我似乎无法得到不等于过滤器的工作:如何在Angular中过滤嵌套对象不等于?

我可以做

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

回答

6

你可以用ng-if得到这样的效果相同:

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

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

+0

我不知道它是否是一个新功能,但有一种方法可以在嵌套结构中使用'!'。 用另一个答案 – 2015-01-28 17:34:56

+0

好的,也许他们已经修好了。太好了! – 2015-01-29 06:33:44

4

你可以使用

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

这是有用的情况下,你不能使用ng-if

<select ng-options="element in elementList | filter: !{property: {text:'yes'} }" /> 

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