简体   繁体   English

角度js默认选中单选按钮

[英]angular js default checked radio button

I know this has probably been asked many times before and I have been through the solutions but none seemed to have worked for me... 我知道这个问题以前可能已经被问过很多遍了,我已经解决了所有问题,但是似乎没有一个对我有用。

html file: html文件:

<input id="sampleA" name="try" type="radio" data-ng-model="$ctrl.isSampleA" value="true" data-ng-change="$ctrl.update()"></input>
<label for="sampleA">Sample A</label>
<input id="sampleB" name="try" type="radio" data-ng-model="$ctrl.isSampleA" value="false" data-ng-change="$ctrl.update()"></input>
<label for="sampleB">Sample B</label>

js file: js文件:

//Initialization:
vm.isSampleA = false;
//This function returns the correct boolean value returned by API
    vm.update = function() {
    vm.isSampleA = response.rows["0"].sampleA;
};

Briefly I'll explain the flow: The default check initially is 'Sample B' option. 简要地讲一下流程:默认检查最初是“样本B”选项。 Then the user checks 'Sample A' option and sends the updated value to REST API, where it gets updated correctly. 然后,用户检查“样本A”选项,并将更新后的值发送到REST API,在此它会正确更新。 Next when I want to edit this form again: 接下来,当我想再次编辑此表单时:

Expected: 'Sample A' should be checked 预期:应检查“样品A”

Actual: none of the 2 options are checked, even though the value of 'isSampleA' is true. 实际:即使'isSampleA'的值为true,也不会选中这两个选项。

Use ng-value directive instead of plain value attribute: 使用ng-value指令代替纯value属性:

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script> angular.module('app', []) </script> <div ng-app='app' ng-init='test=true'> <label> <input type="radio" ng-model="test" ng-value="true"> One </label> <br/> <label> <input type="radio" ng-model="test" ng-value="false"> Two </label> <br/> <button ng-click='test = !test'>Click</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM