我知道这很简单,但只是烦我,我得到了以下代码
<label>Role</label><div>
<select ng-change="save()" ng-model="frm.TypeID">
<option ng-value="">Select Role</option>
<option ng-value="1" value="1">Admin</option>
<option ng-value="3" value="3">User</option>
<option ng-value="2" value="2">Guest</option>
</select>
我从数据库中获取ng-value但是无法通过控制器更新它,所以我该怎么做呢
angular.controller('testCtrl',['$scope', function($scope){
$scope.select=function(){
$scope.frm.TypeID = 3;
}
}]);
由于
答案 0 :(得分:2)
使用ng-options
:
<强>模板强>
<select ng-model="frm.TypeID" ng-options="role.id as role.label for role in roles">
<option ng-value="">Select Role</option>
</select>
<强>控制器强>
$scope.roles = [
{id: 1, label: 'Admin'},
{id: 2, label: 'User'},
{id: 3, label: 'Guest'}
];
$scope.frm = {
TypeID: 3
};
// fetch value from database then update value
fetchData().then(function (id) {
$scope.frm.TypeID = id;
}
参见 fiddle