![](/img/trans.png)
[英]angular select ng-selected not working (with <option ng-repeat>)
[英]ng-selected with ng-repeat in select
抱歉再次询问。 但是我不能为select设置默认值。 这是我使用的代码
普伦克
在这种情况下,我不能使用ng-options。 请帮我没有ng-options
如果模型的值与定义的选项相同,则ng-selected
工作方式为:
ng-selected="business_entity == businessConstants"
鉴于模型是这样的:
$scope.formData = {business_entity : 'Công ty TNHH'};
尝试如下使用控制器中的代码,
$scope.formData={};
$scope.formData.business_entity = 'Công ty TNHH';
并且请从您的模板中的选项标签中删除“ ng-selected”。
<select ng-model="formData.business_entity">
<option ng-repeat="businessConstants in businessConstants" value="{{businessConstants}}">
{{businessConstants}}
</option>
</select>
我已更新您的订单,请检查。
删除ng-select
并将数组引用分配给ng-model
变量
$scope.formData = {
'business_entity' : $scope.businessConstants[1]
}
HTML
<select name="" id=""
ng-model="formData.business_entity">
<option
ng-repeat="businessConstants in businessConstants"
value="{{businessConstants}}">
{{businessConstants}}
</option>
</select>
将您的HTML更改为
<body ng-controller="ctrl">
<select name="" id=""
ng-options="option for option in businessConstants"
ng-model="business_entity">
</select>
</body>
您的选项将自动生成,所选内容将自动绑定到business_entity
将控制器更改为
function ctrl($scope) {
$scope.businessConstants = [
'Công ty Cổ phần',
'Công ty TNHH',
'Tổ chức nhà nước',
'Ngân hàng',
'Trường học',
'Cá nhân',
'Khác'
];
$scope.business_entity = $scope.businessConstants[4];
}
这将设置默认值。 确保您从现有集合businessConstants
选择默认值,然后不分配它。
您可以参考更新的Plunker链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.