我想在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"}];
我想加载&#34;美利坚合众国&#34;最初
目前无效。帮我完成这件事......
答案 0 :(得分: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);
}
});
});