我正在尝试使用angularjs将默认值设置为下拉列表。我粘贴了下面的代码。 这是viewmodel代码和html代码:
appmodule.factory("testpage1ViewModel", function() {
function Testpage1ViewModel () {
this.SearchContainer1 = {
cmbGender:''
};
this.message = '';
this.SearchContainer1_Master_cmbGender = {
ValidValues: [
{ id: '1', name: 'Male' },
{ id: '2', name: 'Female' },
{ id: '3', name: 'Transgender' }
],
DefaultValue: { id: '1', name: 'Male' }
};
}
return Testpage1ViewModel;
});
<select name="cmbGender"
class="form-control"
ng-options="option.name for option in viewModel.SearchContainer1_Master_cmbGender.ValidValues track by option.id"
ng-model="viewModel.SearchContainer1.cmbGender"></select>
答案 0 :(得分:0)
使用ng-init。见下面的例子:
在HTML中
<select ng-change="fruitselect(fruitname)" ng-model="fruitname" ng-init="fruitname = fruits[0]" >
<option ng-repeat="fruit in fruits" value="{{fruit}}">{{fruit}}</option>
</select>
在Controller中,
$scope.fruits = ['apple', 'orange','banana', 'grapes', 'plum'];
答案 1 :(得分:0)
如果您现有的代码是正确的,那么您可以像这样定义ng-model:
<select ng-model="DefaultValue.name" name="cmbGender" class="form-control" ng-options="option.name for option in viewModel.SearchContainer1_Master_cmbGender.ValidValues track by option.id"></select>