oca*_*ian 12 angularjs ng-repeat angularjs-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"}
]
Run Code Online (Sandbox Code Playgroud)
然后我使用该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>
Run Code Online (Sandbox Code Playgroud)
但是,当我运行代码时,我只显示以下内容:
位置复选框 {{country.name}}
如果我删除ng-model
重复的部分我的复选框生成正常但我需要一个唯一的ng-model
附加到每个复选框
ng-model="{{country.id}}"
Run Code Online (Sandbox Code Playgroud)
我如何附加一个独特的ng-model
价值?
这个答案(在ng-repeat中生成ng-model)不提供唯一ng-model
值
Ved*_*Ved 25
我会建议你,使用:
<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:{}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28089 次 |
最近记录: |