如何将特定值设置为下拉角度方式?

时间:2016-06-13 07:23:50

标签: javascript jquery angularjs

我有一个简单的角度下拉,如下:

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

但它没有用。怎么做?

2 个答案:

答案 0 :(得分:2)

在使用track by表达式时,您需要设置与OwnerLevelID关联的对象的ngModel属性。

$scope.docPropIdentityModel.OwnerLevel = { OwnerLevelID : "123456"};

jsFiddle

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