我想在md-select
中预加载值我有以下代码<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"}];
这里我想首先加载"United States of America"。
假设您的服务返回键"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