当我的模型值为true
时,我希望在加载时选择单选按钮,但它会在其他位置发生。所有false
模型都已被选中。如何解决这个问题。
http://plnkr.co/edit/DIYm4vBM3srdS61K6EPA?p=preview
angular.module('radioExample', [])
.controller('ExampleController', ['$scope',
function($scope) {
$scope.kind = [{
name: 'task',
selected: false
}, {
name: 'bug',
selected: false
}, {
name: 'other',
selected: true
}, {
name: 'rfe',
selected: false
}]
$scope.$watch('kind', function() {
console.log('changed', JSON.stringify($scope.kind, null, 2))
}, true)
}
]);
答案 0 :(得分:10)
使用ng-value
这里是angular radio
<input type="radio" name="" id="" value="" ng-model="k.selected" ng-value="true" />
然后,如果ng-value
为true
且型号值也为true
,则会选中复选框
答案 1 :(得分:3)
<input type="checkbox" id="rempass" ng-model="rememberMe" ng-checked="rememberMeUserInfoCheck()" > $scope.rememberMeUserInfoCheck=function() { return true; }
这对我有用
答案 2 :(得分:1)
我修好了弹药plunker
<form name="myForm" ng-controller="ExampleController">
<br />all 'false' radio buttons are selected when 'value' is used -------------
<br />
<div ng-repeat="k in kind">
<input type="radio" name="" id="" value="" ng-model="!k.selected" value="k.selected" />{{k.name}}
</div>
<br />all radio buttons are selected when 'ng-value' is used -------------
<br />
<div ng-repeat="k in kind">
<input type="radio" name="" id="" value="" ng-model="k.selected" ng-value="k.selected" />{{k.name}}
</div>
</form>
你做对了....只需要添加一个!所以模型将采用与范围值相反的...因为你使用它们两者我猜它不会伤害你的代码
答案 3 :(得分:0)
将 ng-checked =“true”添加到您的电台输入栏
`<input type="radio" ng-model="modelName" name="radioName" value="value1" ng-checked="true">`