简体   繁体   English

AngularJS更改选择选项ng-click

[英]AngularJS change select option ng-click

It is simple i know but just bugging me i got the following code 我知道这很简单,但是只是烦我,我得到了以下代码

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

I get the ng-value from the database but can not update it via the controller so how can i do something like 我从数据库获取ng值,但无法通过控制器更新它,所以我该怎么做

angular.controller('testCtrl',['$scope', function($scope){
 $scope.select=function(){
    $scope.frm.TypeID = 3;
}
}]);

Thanks 谢谢

Use ng-options : 使用ng-options

Template 模板

<select ng-model="frm.TypeID" ng-options="role.id as role.label for role in roles">
    <option ng-value="">Select Role</option>
</select>

Controller 调节器

$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;
} 

See fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM