简体   繁体   中英

Misunderstanding with ng-options

Well I'm seriously confused by ng-options. Why make it so difficult??

Anyway, maybe you good folks can help lift the fog...

I have an array of currencies:

["gbp", "usd", "eur"];

etc stored in:

$scope.providedCurrencies

I am trying to access them and display them 'The Angular Way' with ng-options in a Select menu, like so:

<select ng-model="selectedCurrency"
        ng-options="currency for currency in providedCurrencies"
        ng-init="selectedCurrency=gbp">
</select>

This will then update a series of payment options to display the different monetary values, based on the currency selected.

The problems:

The format for writing the ng-options doesn't make a lot of sense to me, even after the Angular docs and various tutorials :(

Also even with the ng-init (from a tutorial) the Select menu will set an empty value on page load. This is then removed when the user selects a different value.

Additionally the value of each option in the DOM is "string:gbp" .

There seems to be more information around when dealing with data in objects over arrays. Should I do that? Even though the docs show options for arrays?

Please help :)

You were forget to do single quote only:

<select ng-model="selectedCurrency"
    ng-options="currency for currency in providedCurrencies"
    ng-init="selectedCurrency='gbp'">
</select>

Check this: http://codepen.io/anon/pen/RrNyvb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM