我使用ng-repeat="(key, value)"
語法來迭代JSON對象,但在使用複選框時我實際上並不需要value
變量。有沒有更合適的方法來做到以下幾點:只有在迭代JSON對象時才需要鍵的重複語法
編輯: HAML混淆了一些,所以這裏是純HTML /角:我實際使用attribute
爲ng-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],而在ng-repeat你的關鍵是屬性,這是錯字 –
這是一個錯字在翻譯的HTML,但不影響實際問題。上面的HAML在瀏覽器中工作,當我簡化我的copy.paste – iftheshoefritz
而不是ng-model =「object [attribute]」時,出錯了,你可以寫ng-model =「value」,所以不需要這裏的屬性 –