我有一个简单的角度下拉,如下:
<select class="form-control" ng-model="docPropIdentityModel.OwnerLevel"
ng-options="ownerLevel as ownerLevel.LevelName for ownerLevel in ownerLevels track by ownerLevel.OwnerLevelID">
<option value="">--Select--</option>
</select>
我有一个OwnerLevelID的值,我想将OwnerLevelID指定为下拉列表的值。显示相应的LevelName。我可以通过使用jquery轻松完成此操作,但希望以有角度的方式进行。
我尝试为模型赋值如下:
$scope.docPropIdentityModel.OwnerLevel = "123456";
但它没有用。怎么做?
答案 0 :(得分:2)
在使用track by
表达式时,您需要设置与OwnerLevelID
关联的对象的ngModel
属性。
$scope.docPropIdentityModel.OwnerLevel = { OwnerLevelID : "123456"};
答案 1 :(得分:1)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script>
angular.module("myapp", [])
.controller("MyController", function ($scope) {
$scope.register = {};
$scope.register.countryId = "4";
$scope.register.countries = [{
id: "1",
name: "India"
}, {
id: "2",
name: "USA"
}, {
id: "3",
name: "UK"
}, {
id: "4",
name: "Nepal"
}];
});
</script>
</head>
<body ng-app="myapp">
<div ng-controller="MyController">
<div>
Country Name : <select ng-model="register.countryId" ng-options="country.id as country.name for country in register.countries"></select>
</div>
<div>
Country-Id : {{register.countryId}}
</div>
</div>
</body>
</html>