2014-10-03 42 views
0

我有一個JSON對象,它包含一些namespace:key形式的鍵中的名稱空間。因此,我需要使用方括號符號而不是點符號來訪問它的值。在Angular指令中使用JSON方括號表示法來訪問值

我想使用一些角度指令/過濾器來處理數據,在這個例子中我將使用filter,但這個問題似乎橫向於在指令中使用括號表示法。

這裏是我的例子:

NG重複使用過濾器在HTML:

<ul> 
    <li ng-repeat="item in items | filter:{properties['namespace:status']}"> 
    {{item.name}} 
    </li> 
</ul> 

$ scope.items內容:

{ 
    "properties":{ 
    "namespace:status":"A", 
    "name":"Potato", 
    // ... 
    }, 
    // ... 
}, 
{ 
    "properties":{ 
    "namespace:status":"B", 
    "name":"Carrot", 
    // ... 
    }, 
    // ... 
}, 
{ 
    "properties":{ 
    // Note that it doesn't contain the namespace attribute 
    "name":"Bean", 
    // ... 
    }, 
    // ... 
} 

預計渲染HTML:

<ul> 
    <li>Potato</li> 
    <li>Carrot</li> 
</ul> 

實際結果

Error: [$parse:syntax] Syntax Error: Token '[' is unexpected, expecting [:] at column X of the expression [items | filter:{properties['namespace:status']}] starting at [['namespace:status']}]. 

,我向語法錯誤。

我的問題是,相同的表達式{{properties['namespace:status']}}不符合模板中的語法,所以我不知道我做錯了什麼,或者如果指令根本無法處理括號表示法。

如果它的後者,有關如何在不重寫JSON的情況下處理這個問題的建議?

回答

1

應指定這樣的

<ul> 
    <li ng-repeat="item in items | filter:{prop: properties['namespace:status']}"> 
    {{item.name}} 
    </li> 
</ul> 

這裏的對象鍵,您可以檢查http://codepen.io/anon/pen/BslpA

但你應該知道,angularjs無法通過對象屬性的濾鏡陣列,它只能是數組。

+0

就是這樣。關於對象屬性數組,我可能會使用不同的指令 – batista 2014-10-06 10:07:06

相關問題