如何在angualrjs中显式设置选择选项 [英] How to set select option explicitly in angualrjs
问题描述
大家好,
我正在处理一个代码并且卡在一点上。因为我是angularjs的新手,我需要一些指导。
我在尝试什么要做的是我想在UI上显示数据库中的一些值。为了从db中获取数据,我使用了返回一些数据的web服务。我想在ui上的组合框中将该值显示为选定值。
这里我分享这段代码Html页面和js页面。
提前完成。
我尝试了什么: < br $> b $ b
我到现在为止尝试的是
****** html代码****
Hello everyone,
I am working on a code and got stuck at one point.Since i am new to angularjs i need some guidance.
What i am trying to do is that i want to display some values from database on the UI. In order to fetch data from db i had used web service which return some data.I want to display that value as a selected value on the combo box on ui.
Here i am sharing the piece of code of both Html page and that of js.
Thanxs in advance.
What I have tried:
What i have tried till now is
******html code****
<select name="userType" id="userType" class="form-control"
ng-model="user.UserType" required>
<option value="">-Select option-</option>
<option ng-repeat="UT in UserTypes" value="{{UT.Id}}" ng-selected="{{ UT.Selected == true }}">
{{UT.Name}}
</option>
</select>
**** Js代码********
****Js Code********
$scope.UserTypes = [{
Id: 1,
Name: 'Admin',
Selected: false
}, {
Id: 2,
Name: 'Doctor',
Selected: false
}, {
Id: 3,
Name: 'Receptionist',
Selected: false
}, {
Id: 4,
Name: 'Nurse',
Selected: false
}, {
Id: 5,
Name: 'Staff',
Selected: false
}];
angular.forEach($scope.UserTypes, function (value, key) {
alert(value.Id + " ," + $scope.user.UserType);
if (value.Id == $scope.user.UserType) {
value.Selected = true;
alert("true");
}
});
************************
************************
推荐答案
scope.UserTypes = [{
Id: 1 ,
名称: ' 管理员',
已选中: false
},{
Id: 2 ,
名称:' Doctor',
Sele cted: false
},{
Id: 3 ,
名称:' 接待员',
已选择: false
},{
Id: 4 ,
名称: ' 护士',
已选择: false
},{
Id: 5 ,
名称:' 员工,
已选择: false
}];
angular.forEach(
scope.UserTypes = [{ Id: 1, Name: 'Admin', Selected: false }, { Id: 2, Name: 'Doctor', Selected: false }, { Id: 3, Name: 'Receptionist', Selected: false }, { Id: 4, Name: 'Nurse', Selected: false }, { Id: 5, Name: 'Staff', Selected: false }]; angular.forEach(
scope.UserTypes, function (value,key ){
alert(value.Id + , +
scope.UserTypes, function (value, key) { alert(value.Id + " ," +
scope.user.UserType);
if (value.Id ==
scope.user.UserType); if (value.Id ==
这篇关于如何在angualrjs中显式设置选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!