gpt4 book ai didi

javascript - AngularJS 更改选择选项 ng-click

转载 作者:行者123 更新时间:2023-11-30 16:32:01 26 4
gpt4 key购买 nike

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

<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 值,但无法通过 Controller 更新它,所以我该怎么做

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

谢谢

最佳答案

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

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

参见 fiddle

关于javascript - AngularJS 更改选择选项 ng-click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com