2016-09-11 6 views
0

私はMD-選択角度材料選択で値をどのようにプリロードすることができますか?

に値を事前ロードするI持って、私は「米国をロードするここで

$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"}]; 

次のコード

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

コントローラ"最初は。

現在動作していません。これを成し遂げるために私を助けて...

答えて

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

関連する問題