Angularjs 将ng模型指定给ng repeat生成的复选框
我已经设置了一个json,其中包含一个附加ID和国家代码的国家列表: 看起来是这样的:Angularjs 将ng模型指定给ng repeat生成的复选框,angularjs,angularjs-ng-repeat,ng-repeat,Angularjs,Angularjs Ng Repeat,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":"Al
$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>
我将如何附加唯一的ng模型
值
这个答案()没有提供唯一的
ng模型
值我建议您,使用:
<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:{}
};
{{country.name}
{{myCountry.selected}
JS:
$scope.myCountry={
所选:{}
};
尝试删除{{}}
在{country.id}}}
周围的{{{country.id}}
执行此操作时,所有复选框的ng模型值都相同,但我需要每个复选框的唯一ng模型值尝试设置ng重复
到国家/地区按$index跟踪
和ng模型
到国家/地区[$index]
这似乎也只为所有复选框提供了相同的ng模型-ng model=“countries[$index]”这是我认为可行的方法,但出于某种原因,我每次重复复选框都会得到相同的ng模型值。我正在获取myCountry。为每个checboxI猜测选择[country.id],您将通过检查元素来检查它。它将显示相同的结果。将模型传递给控制器并控制结果。你会得到你的答案。哦,你是对的。我正在使用inspect元素。谢谢你的帮助对我有用。但是如何在myCountry.selected中迭代?@AnushreeAcharjee你面临什么问题?迭代是容易的,对吗?
<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:{}
};