Everything lists out fine, and I have the default value set in the model, but it seems that it just does not want to use that as the default option in the select element. How do I accomplish getting the model to select the corresponding language iso value? Also, how do I keep from an empty option being added to the the select?
Example data:
// 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"
}
Markup:
<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) {});
};
The result:
<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>
The only way to fix the empty option issue is to assign a default value in your controller. It's pretty standard these days to just use the first value, so just update your $scope.init
to:
$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) {});
};