很抱歉再次询问。但是我无法为select设置默认值。这是我用过的代码
Plnkr
在这种情况下,我无法使用ng-options。请帮助我没有ng-options
答案 0 :(得分:1)
如果模型的值与定义的选项相同,ng-selected
将起作用:
ng-selected="business_entity == businessConstants"
鉴于模型是这样的:
$scope.formData = {business_entity : 'Công ty TNHH'};
答案 1 :(得分:1)
尝试使用控制器中的代码,如下所示
$scope.formData={};
$scope.formData.business_entity = 'Công ty TNHH';
请从你的tempalte中删除你的选项标签中的'ng-selected'。
<select ng-model="formData.business_entity">
<option ng-repeat="businessConstants in businessConstants" value="{{businessConstants}}">
{{businessConstants}}
</option>
</select>
我已更新您的plnk,请检查..
答案 2 :(得分:1)
删除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>
答案 3 :(得分:1)
将您的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 link