我已经设置了一个包含国家ID和国家代码的国家列表json:
它看起来像这样:
它看起来像这样:
$scope.countries = [
{"name":"Afghanistan","id":"AFG","country-code":"004"},
{"name":"Åland Islands","id":"ALA","country-code":"248"},
{"name":"Albania","id":"ALB","country-code":"008"},
{"name":"Algeria","id":"DZA","country-code":"012"}
]
然后我使用 ng-repeat
指令为每个国家创建复选框输入。
<div ng-repeat="country in countries">
<label><input type="checkbox" ng-model="{{country.id}}" ng-true-value="'{{country.name}}'" ng-false-value="''">{{country.name}}</label>
</div>
然而,当我运行代码时,只有以下内容显示:
位置
这里有一个复选框 {{country.name}}
如果我删除重复部分的 ng-model
,复选框就可以正常生成,但我需要为每个复选框附加独特的 ng-model
。
ng-model="{{country.id}}"
我该如何附加一个唯一的ng-model
值?
这个答案(在ng-repeat内生成ng-model)没有提供一个唯一的ng-model
值。
{{country.id}}
周围的{{}}
。 - simegng-repeat
设置为country in countries track by $index
,并将ng-model
设置为countries[$index]
。 - simeg