AngularJS更改选择选项ng-click

时间:2015-10-17 22:51:40

标签: javascript angularjs

我知道这很简单,但只是烦我,我得到了以下代码

<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;
}
}]);

由于

1 个答案:

答案 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