2016-06-13 136 views
0

我有一個簡單的角度下拉像波紋管:如何將特定值設置爲下拉角度方式?

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

但沒有奏效。如何做到這一點?

+1

'$ scope.docPropIdentityModel.OwnerLevel = {OwnerLevelID: 「123456」};' – Satpal

+0

你確定'OwnerLevelID'是'string'而不是'number'? '$ scope.docPropIdentityModel.OwnerLevel = 123456;' – Kutyel

回答

2

當您使用track by表達,您需要設置與ngModel相關對象的OwnerLevelID財產。

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

jsFiddle

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> 
相關問題