I have tried to follow the examples on the Angular 1.4.12 docs and what I've found here, still I cannot set a default value for a select menu.
My html (using controller as
of mob
):
<select name="userCurrencyType"
ng-model="mob.currencyType"
ng-options="s.name + ' - ' + s.code for s in mob.currencyTypes"></select>
Which correctly gives me a menu like:
Where my json is an array of objects:
[{ "code": "USD", "name": "United States Dollar" },
{ "code": "GBP", "name": "United Kingdom Pound" }...]
I want the default menu item to be the first item (USD). I have tried setting the ng-model
to mob.currencyType
and setting this in the controller both like:
_this.currencyType = _this.currencyTypes[0];
and
_this.currencyType = { "code": "USD", "name": "United States Dollar" }
Neither approach gives me a default value set. What am I missing?
UPDATE
After some good suggestions from other users, and some experimenting, it would seem the problem was my data service call was not returning a promise:
_this.currencyTypes = MockDataFactory.query({ filename: 'currency_codes' });
So I added
_this.currencyTypes.$promise.then(function () {
init();
});
And then
function init () {
_this.currencyType = _this.currencyTypes[0];
}