[英]How to pre-select radio buttons in angularjs
当我的模型值为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)
}
]);
<input type="checkbox" id="rempass" ng-model="rememberMe" ng-checked="rememberMeUserInfoCheck()" > $scope.rememberMeUserInfoCheck=function() { return true; }
这对我有用
我修理了pl 子
<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>
您说对了...。只需添加一个! 因此该模型将采用范围值的反面...因为您同时使用它们,我想它不会伤害您的代码
将ng-checked =“ true”添加到您的单选框
`<input type="radio" ng-model="modelName" name="radioName" value="value1" ng-checked="true">`
FormGroup
和FormControl
:formControlName = sameNameforAllRadioButtonsOfAChoice
作为单选按钮input
标签的属性。value = "true"
和value = "false"
(你也可以用数字和字符串来做,但我会继续用布尔值)。sameNameforAllRadioButtonsOfAChoice
添加到您的 FormGroup 中: myForm = new FormGroup({sameNameforAllRadioButtonsOfAChoice: new FormControl('false')})
这将默认值设置为false
。 在FormControl中,注意写成字符串!
import { FormControl, FormGroup, Validators } from '@angular/forms';
myForm = new FormGroup({sameNameforAllRadioButtonsOfAChoice: new FormControl('false', [Validators.required])})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.