I populate my select with an external json-file. Everything works fine. But how can I set the default value of my select? I want to set it to "United States", one of my option-value, which is loaded from the json-file countries.json.
I´ve tried to do it like this:
ng-init="selectionCountry = options['United States']
or with a no., because the States are on the first position...
ng-init="selectionCountry = options[0]
But, it do not work. Any tips?
HTML
<select id="country" class="form-control country" ng-model="selectionCountry">
<option value="" ng-options="country in data.locations.countries | orderBy:'name'">{{country.name}}</option>
</select>
SCRIPT
.controller('NewInvoiceCtrl', ['$scope', '$translate', '$modal', '$window', '$filter', '$http', '$timeout',
function($scope, $translate, $modal, $window, $filter, $http, $timeout) {
// load and populate Json in country select
$scope.data = {
"locations": {}
};
$http.get('l10n/countries.json')
.then(function(res) {
$scope.data.locations.countries = res.data;
$scope.$$phase || $scope.$apply();
// set default
$scope.selectionCountry = $scope.data.locations.countries[1];
});
}])
EXAMPLE JSON
{
"alpha2": "US",
"alpha3": "USA",
"countryCallingCodes": [
"+1"
],
"currencies": [
"USD"
],
"ioc": "USA",
"name": "United States",
"status": "assigned"
},
data.locations.countries. Why do you think setting the selected value tooptions['United States']oroptions[0]would work? Wouldn't it be much more logical to set it todata.locations.countries[0]?