这是我的代码:
<div ng-controller="TestController">
<input ng-repeat="item in array" ng-model="selected.name" value="{{item.name}}" type="radio"></input>
</div>
<script type="text/javascript">
var app = angular.module('app', []);
app.controller('TestController', function ($scope) {
$scope.array = [{
name: 'lee',
seq: 1,
}, {
name: 'tom',
seq: 2,
}, {
name: 'jack',
seq: 3,
}];
$scope.selected = $scope.array[0];
});
</script>
显示页面时,默认选中的单选框是正确的。但它不能不检查,我只能在另外两个复选框之间切换?我该如何解决这个问题?
答案 0 :(得分:1)
ng-repeat创建新范围,以便确定父范围。
了解更多信息,请参阅https://docs.angularjs.org/api/ng/directive/ngRepeat
var app = angular.module('app', []);
app.controller('TestController', function ($scope) {
$scope.array = [{
name: 'lee',
seq: 1,
}, {
name: 'tom',
seq: 2,
}, {
name: 'jack',
seq: 3,
}];
$scope.selected = $scope.array[0].name;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="TestController">
<div ng-repeat="item in array">
<input type="radio" ng-model="$parent.selected"
ng-value="item.name" >
</div>
</div>