一切都很好,我在模型中设置了默认值,但似乎它不想将它用作select元素中的默认选项。如何让模型选择相应的语言iso值?另外,如何保留添加到选择的空选项?
示例数据:
// list of languages
[
{"iso":"abk","language":"Abkhazian"},
{"iso":"aar","language":"Afar"},
{"iso":"afr","language":"Afrikaans"},
...
]
// location object containing the property for the language value
{
"id":"516",
"site":"Al Seela",
"start_utc":"03:00:00",
"end_utc":"06:00:00",
...
"lang_iso":"eng",
"language":"English"
}
标记:
<div ng-controller="StationListController as stationList" ng-init="init()">
...
<div ng-repeat="location in station.locations" class="panel panel-default">
...
<select class="form-control" id="{{'locationLang-' + location.id}}" ng-model="location.lang_iso" ng-options="lang.iso as lang.language for lang in locLanguages track by lang.iso"></select>
JS:
$scope.init = function () {
$http.get($scope.ajaxurl + '?_mode=languages&_task=list').success(function(data, success) {
$scope.locLanguages = data;
}).error(function(error) {});
};
结果:
<select ng-options="lang.iso as lang.language for lang in locLanguages track by lang.iso" ng-model="location.lang_iso" id="locationLang-844" class="form-control ng-pristine ng-valid ng-touched">
<option value="?" selected="selected"></option>
<option value="abk" label="Abkhazian">Abkhazian</option>
<option value="aar" label="Afar">Afar</option>
<option value="afr" label="Afrikaans">Afrikaans</option>
...
</select>
答案 0 :(得分:1)
解决空选项问题的唯一方法是在控制器中分配默认值。这些天使用第一个值非常标准,所以只需将$scope.init
更新为:
$scope.init = function () {
$http.get($scope.ajaxurl + '?_mode=languages&_task=list').success(function(data, success) {
$scope.locLanguages = data;
$scope.location.lang_iso = {"iso":"abk","language":"Abkhazian"}; // or perhaps the entire object
}).error(function(error) {});
};