将 ng-model 分配给 ng-repeat 生成的复选框

Assigning ng-model to checkboxes generated by ng-repeat

我已经设置了一个 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

我会建议你,使用:

<div ng-repeat="country in countries">
  <label><input type="checkbox" ng-model="myCountry.selected[country.id]" ng-true-value="'{{country.name}}'" ng-false-value="''">{{country.name}}</label>
</div>

{{myCountry.selected}}

JS:

$scope.myCountry = {
    selected:{}
};