2016-09-11 53 views
0

我要预先加载在MD-选择如何预加载角度材料选择中的值?

值我有以下代码

<md-select ng-model="country" ng-model-options="{trackBy: '$value.code'}" > 
    <md-optgroup label="Country"> 
     <md-option ng-value="country" ng-repeat="country in countries"> 
        {{country.name}} 
     </md-option> 
    </md-optgroup> 
</md-select> 

控制器

$scope.country = "usa"; //I will receive only the key from the server side 
         //in the response object 

$scope.countries = [{"code":"usa","name":"United States of America"}, 
    {"code":"ind","name":"India"}, 
    {"code":"eng","name":"England"},  
    {"code":"aus","name":"Australia"}]; 

在这里,我要加载“美国“ 原来。

目前无法正常工作。帮我完成这件事......

回答

1

假设你的服务返回键"usa",它会检查关键的下拉阵列的存在和分配对象到$scope.country

app.controller('myCtrl', function($scope) { 
$scope.service = "usa"; 
$scope.countries = [{"code":"usa","name":"United States of America"}, 
    {"code":"ind","name":"India"}, 
    {"code":"eng","name":"England"},  
    {"code":"aus","name":"Australia"}]; 

    angular.forEach($scope.countries, function(value) { 
     if (value.code === $scope.service) { 
      $scope.country ={code: $scope.service , name: value.name}; 
      console.log($scope.country); 
     } 
    }); 

}); 

WORKING DEMO

+0

我想只发送选定对象的代码。 ng-value =“country.code”不预先加载数据。另外选择不起作用。如果我选择任意一个国家/地区,请选择下拉菜单中的最后一个条目。我怎样才能只发送密钥 – Prince