答案 0 :(得分:1)
为什么不使用ng-options
?
<select ng-model="form.isAdmin" ng-options="o.value as o.name for o in options"></select>
控制器中的 options
数组:
$scope.options = [
{ 'name': 'Yes', 'value': true },
{ 'name': 'No', 'value': false }
];
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', [function() {
var vm = this;
vm.isAdmin = false;
vm.adminOptions = [{
val: true,
name: 'Yes'
}, {
val: false,
name: 'No'
}];
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MyCtrl as form">
<select ng-model="form.isAdmin" ng-options="opt.val as opt.name for opt in form.adminOptions">
</select>
{{form.isAdmin}}
</div>
</div>
&#13;
答案 1 :(得分:1)
使用ng-options
:
<select ng-model="form.isAdmin"
ng-options="opt.val as opt.name for opt in [{val:true,name:'Yes'},{val:false,name:'No'}]">
</select>
答案 2 :(得分:1)
您需要基本使用以下语法作为选项标记(使用ng-selected):
<select ng-model="formCtrl.isAdmin">
<option ng-value="true" ng-selected="formCtrl.isAdmin">Yes</option>
<option ng-value="false" ng-selected="!formCtrl.isAdmin">No</option>
</select>