分配NG-模型吴重复产生的复选框 [英] Assigning ng-model to checkboxes generated by ng-repeat

查看:80
本文介绍了分配NG-模型吴重复产生的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了包含国家名单附有一个ID和乡村code一个JSON:

I have set up a json containing a list of countries with an ID and Country code attached:

它看起来是这样的:

$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-重复指令为每个国家创造复选框输入。

I then use the ng-repeat directive to create checkbox inputs for every country.

<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>

然而,当我运行code我只得到了以下内容显示:

However when I run the code I only get the following to display:

地点
这里的复选框 {{} country.name}

如果我删除的重复我的复选框产生很好,但我需要一个唯一的 NG-模型 NG-模型部分来被连接到每一个复选框

If I remove the ng-model part of the repeat my checkboxes generate fine but I need a unique ng-model to be attached to every checkbox

ng-model="{{country.id}}"

我怎么会去安装一个独特的 NG-模型价值?

这答案(生成NG重复内NG-模式)不不能提供一个独特的 NG-模型

This answer (Generate ng-model inside ng-repeat) does not provide a unique ng-model value

推荐答案

我会建议你,使用:

<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>

</div>
{{myCountry.selected}}

JS:

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

这篇关于分配NG-模型吴重复产生的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆