【发布时间】:2021-12-18 01:26:50
【问题描述】:
我设置了一个 json,其中包含一个国家列表,并附有 ID 和国家代码:
看起来像这样:
$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 值?
此答案 (Generate ng-model inside ng-repeat) 没有提供唯一的 ng-model 值
【问题讨论】:
-
尝试删除
{{country.id}}周围的{{}} -
当我这样做时,我会为所有复选框获得相同的 ng-model 值,但我需要为每个复选框设置一个唯一的 ng-model 值
-
尝试将
ng-repeat设置为country in countries track by $index和ng-model设置为countries[$index] -
这似乎也只为所有复选框提供了相同的 ng-model - ng-model="countries[$index]"
标签: angularjs angularjs-ng-repeat ng-repeat