【发布时间】:2016-09-27 10:21:41
【问题描述】:
我试图在一个循环中填充多组单选按钮,并使用组名和索引的组合作为名称,以便对单选按钮进行唯一分组。问题是 - 只有循环中的最后一组选中了单选按钮。循环中的其他组没有检查任何内容。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - Dynamically populate Radio buttons in a loop</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
</head>
<body ng-app="radioSetsExample">
<script>
angular.module('radioSetsExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.groups = [{
name: 'group_one',
type: 'CHOOSE'
}, {
name: 'group_two',
type: 'ADD'
},
{
name: 'group_three',
type: 'ADD'
}];
}]);
</script>
<form ng-controller="ExampleController">
<div class="radio-inline" ng-repeat="group in groups">
{{group.name}} - TYPE:
<label>
<input type="radio" name="{{group.name}}_{{$index}}" value="CHOOSE" ng-model="group.type">
Choose
</label>
<label>
<input type="radio" name="{{group.name}}_{{$index}}" value="ADD" ng-model="group.type">
Add
</label>
</div>
</form>
</body>
</html>
如果我手动为循环中的每个组提供唯一的“名称”并且不使用 index.html,一切正常。但我不能这样做,因为列表将是动态的并且没有唯一的名称。我必须依靠索引来为每个组生成唯一的名称。
我在这里错过了什么?
【问题讨论】:
标签: javascript html angularjs radio-button ng-repeat