Javascript 如何使用angular js将选择下拉列表中的值填充为默认值
其他文件即将出现,但选择值未出现 在我的眼睛里可以看到Javascript 如何使用angular js将选择下拉列表中的值填充为默认值,javascript,jquery,html,angularjs,Javascript,Jquery,Html,Angularjs,其他文件即将出现,但选择值未出现 在我的眼睛里可以看到 userService.getUser($scope.userInf, function( data ) // success { $scope.userInf = { username: data.userName, firstname: data.firstName, middlename:
userService.getUser($scope.userInf,
function( data ) // success
{
$scope.userInf = {
username: data.userName,
firstname: data.firstName,
middlename: data.middleName,
lastname: data.lastName,
title: data.title,
organization: data.organization,
role: data.authorization.role,
dateOfBirth:data.dateOfBirth,
save: 'update'
};
},
选一个
分析师
管理
我知道Angular有用于此目的的ng init指令。你为什么不试试ng选项指令?您可以将ng init指令与ng选项并排使用,并且ng选项比ng repeat稍微灵活一些
<select data-ng-model="userInf.role" class="span12 ng-pristine ng-valid ng-valid-required" required="">
<option value="? string:Analyst ?"></option>
<option value="" selected="selected" disabled="disabled">Choose one</option>
<!-- ngRepeat: role in roles --><option data-ng-repeat="role in roles" value="Analyst" class="ng-scope ng-binding">Analyst</option>
<option data-ng-repeat="role in roles" value="Admin" class="ng-scope ng-binding">Admin</option>
</select>
选一个
ng init会将您的ng model(以下定义的选项的值)设置为您想要的任何值,在本例中,将您的ng model设置为第一个选项的值“”。ng选项将使用数组中的值填充其余选项。您可能需要一些设置来在选项中显示正确的名称和值。看这里是的,我会这样做:
var app=angular.module('app',[])
app.controller('exampleCtrl',函数($scope){
$scope.options=[“a”、“b”、“c”]
$scope.selected=$scope.options[1]
})
加载时将选择选项“b”
<select ng-options="o for o in options" ng-model="selected"></select>
{{option.name}
您正在使用IE9浏览器吗?
<select ng-model="userInf.role" ng-options="role for role in roles" ng-init="userInf.role = '' " class="span12 ng-pristine ng-valid ng-valid-required" required="">
<option value="" disabled="disabled">Choose one</option>
</select>
var app = angular.module('app', [])
app.controller('exampleCtrl', function ($scope) {
$scope.options = ["a", "b", "c"]
$scope.selected = $scope.options[1]
})
<select ng-options="o for o in options" ng-model="selected"></select>
<select name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect">
<option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
</select>