我想将radiobutton的默认选中值显示为3。我的代码如下:-
<div class="checkbox-group" ng-init="ac.ServiceCode=3">
<input type="radio" ng-model="ac.ServiceCode" id="acuOne" value="1" ng-change='GetAcuityLevel(1)'>
<label for="acuityone">1</label>
<input type="radio" ng-model="ac.ServiceCode" id="acuTwo" value="2" ng-change='GetAcuityLevel(2)'>
<label for="acuitytwo">2</label>
<input type="radio" ng-model="ac.ServiceCode" id="acuThree" value="3" ng-change='GetAcuityLevel(3)'>
<label for="acuitythree">3</label>
<input type="radio" ng-model="ac.ServiceCode" id="acuFour" value="4" ng-change='GetAcuityLevel(4)'>
<label for="acuityfour">4</label>
<input type="radio" ng-model="ac.ServiceCode" id="acuFive" value="5" ng-change='GetAcuityLevel(5)'>
<label for="acuityfive">5</label>
</div>
但在这种情况下,它总是显示 3,当我删除上面的代码时,它会在单选按钮中显示正确的选定值。我想如果服务返回某个值(1 到 5),则应选择单选按钮,否则应选择默认值 3。
最佳答案
那是因为你有一个 ng-init 可以为你做这件事。
ng-init="ac.ServiceCode=3"
通过这种方式,您的默认值将始终为 3 并始终初始化为该值。
你应该以不同的方式处理 ac.ServiceCode:假设你的服务是一个 $http.get 请求
$http.get('api/myservice').
success(function(data, status, headers, config) {
if(data.valueReturned != null)
$scope.ac.ServiceCode = data.valueReturned;
else
$scope.ac.ServiceCode = 3;
});
};
换句话说,您应该在您的服务中处理您的默认值,而不是在您的 HTML 中。
关于javascript - 在单选按钮angularjs中选择默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38973021/