I have an angular directive like so:
'use strict';
angular.module('countrySelect', [])
.directive('countrySelect', ['$parse', function($parse) {
var countries = [
{ code: "af", name: "Afghanistan" },
{ code: "ax", name: "Åland Islands" },
{ code: "al", name: "Albania" },
{ code: "dz", name: "Algeria" },
];
return {
template: '<select ng-options="c.code as c.name for c in countries">',
replace: true,
link: function(scope, elem, attrs) {
scope.countries = countries;
if (!!attrs.ngModel) {
var assignCountry = $parse(attrs.ngModel);
elem.bind('change', function(e) {
assignCountry(elem.val());
});
scope.$watch(attrs.ngModel, function(country) {
elem.val(country);
});
}
}
}
}]);
And a select field in the template like so:
<div country-select ng-model="citizenship"></div>
I can select an option, and the model gets updated with the country code as intended, but the country name does not appear in the select field unless I choose it again.
In other words, the process goes
- click
- select country
- model gets updated with country code but name does not appear in select field
- click and select again
- name appears in the select field
How do I get the select field to display the country name upon select?