2014-11-04 137 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,但你明白我的意思...)