2014-06-09 78 views
0

我使用ng-repeat="(key, value)"語法來迭代JSON對象,但在使用複選框時我實際上並不需要value變量。有沒有更合適的方法來做到以下幾點:只有在迭代JSON對象時才需要鍵的重複語法

編輯: HAML混淆了一些,所以這裏是純HTML /角:我實際使用attributeng-model(它

<!-- "(key, val) in object" allows me to do everything, but value variable is wasted --> 
<label ng-repeat="(attribute, value) in object"> 
    <input type="checkbox" ng-model="object[attribute]"/> 
    {{attribute}} 
</label> 

公告當僅僅綁定到value和複選框標籤時不能正常工作。這裏沒有逃脫的鑰匙。我不需要的是變量。

這是事實,你可以做ng-repeat: "value in object",但value不上的複選框正確綁定,並再次,不給我點使用的標籤:

<!-- "val in object" does not give me a label for checkboxes, and does not bind correctly --> 
<label ng-repeat="value in object"> 
    <input type="checkbox" ng-model="value"/> <!-- changing this checkbox does not update the parent scope --> 
    {{attribute}} <!-- does not exist in scope --> 
</label> 

這裏是一個小提琴示範以上:http://jsfiddle.net/Zb5bA/8/

+1

你在使用對象[屬性1],而在ng-repeat你的關鍵是屬性,這是錯字 –

+0

這是一個錯字在翻譯的HTML,但不影響實際問題。上面的HAML在瀏覽器中工作,當我簡化我的copy.paste – iftheshoefritz

+0

而不是ng-model =「object [attribute]」時,出錯了,你可以寫ng-model =「value」,所以不需要這裏的屬性 –

回答

2

有沒有辦法只迭代上的鍵,所以你必須使用

ng-repeat="(key, value) in object" 

可以在這裏忽略價值。

+0

我如何將attribute1和attribute2(*鍵*值)作爲標籤使用,而不需要引用鍵?而且,綁定到「value」不會更新控制器中其他位置的數據,因爲ng-repeat的每次迭代都會創建自己的範圍。因爲我在原來的問題中解釋了大部分這個問題。 – iftheshoefritz

+0

刪除我的downvote,感謝您的更新 – iftheshoefritz