在单选按钮angularjs中选择默认值
Selecting default value in radio button angularjs
我想显示单选按钮的默认选择值为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 = "交流。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中。
尝试替换这一行:
<div class="checkbox-group" ng-init="ac.ServiceCode=3">
与这个:<div class="checkbox-group" ng-init="ac.ServiceCode = ac.ServiceCode ? ac.ServiceCode : 3">
使用三元运算符检查值是否为真,否则默认为3。
你可以这样做,不要使用ng-init, ng-if会解决你的问题。当服务返回值大于选择的值时,则选择3。
尝试像$scope那样改变它。Ac = {};美元的范围。ac = {ServiceCode:4}并观察其行为
var app = angular.module('myapp',[]);
app.controller('appCtrl',function($scope){
$scope.ac = {};
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="appCtrl">
<div class="checkbox-group" ng-if="!(ac.ServiceCode)?ac.ServiceCode=3:ac.ServiceCode">
<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>
</div>
相关文章:
- Selectize选择默认值
- 在多选下拉列表中选择默认值
- 在 Angular JS 中为 ng 选项选择默认值
- 如何从 AngularJS 的下拉框中设置选择默认值
- 当 ng-model 和 ng-options 引用不同的对象时,如何使用 ng-init 在选择框中选择默认值
- 为“角度”中的下拉列表选择默认值
- 在 html 下拉列表中选择默认值
- 在angularjs中选择,而不是选择默认值
- easyUI数据网格内部编辑组合框无法选择默认值
- 角度设置ui从另一个控制器中选择默认值
- 在ng-options中选择默认值
- 防止在ASP.NET中选择默认值时下拉的“选定索引更改”事件
- 我的javascript函数在下拉菜单中选择默认值有什么问题?
- 动态选择并选择默认值
- 当从下拉菜单中选择默认值时,隐藏复选框.[Angular js]
- Angularjs使用ng-options选择默认值
- 不能在AngularJS中使用ng-model和ng-repeat或ng-select在下拉菜单中选择默认值
- 在单选按钮angularjs中选择默认值
- Angular.js:从ID中选择默认值
- AngularJS ng选项在选择选项后选择默认值导致:“;TypeError:无法读取属性'[属性]'