Exploring how to use AngularJS ng-option directive to populate a select menu and set a default option.
The JSON data retrieved by AngularJS:
{"AF":"Afghanistan","AX":"\u00c5land Islands","AL":"Albania","DZ":"Algeria","AS":"American Samoa","AD"}
The code for populating the select menu:
angular.module('drpdwnApp', []).controller('drpdwnCtrl', function ($scope, $http) {
$scope.selectedCountries = null;
$scope.countries = [];
$http({
method: 'GET',
url: '/api/getCountries'
}).success(function (result) {
$scope.countries = result;
});
});
The HTML structure of the select menu:
<select name="property_country" ng-model="drpdpwnvalue" ng-options="key as value for (key , value) in countries track by key">
<option value="">Please choose</option>
</select>
If we want to set "AL" as the default selected option, how can we accomplish this?