ng-options with simple array init ng-options with simple array init mongoose mongoose

ng-options with simple array init


You actually had it correct in your third attempt.

 <select ng-model="myselect" ng-options="o as o for o in options"></select>

See a working example here:http://plnkr.co/edit/xEERH2zDQ5mPXt9qCl6k?p=preview

The trick is that AngularJS writes the keys as numbers from 0 to n anyway, and translates back when updating the model.

As a result, the HTML will look incorrect but the model will still be set properly when choosing a value. (i.e. AngularJS will translate '0' back to 'var1')

The solution by Epokk also works, however if you're loading data asynchronously you might find it doesn't always update correctly. Using ngOptions will correctly refresh when the scope changes.


You can use ng-repeat with option like this:

<form>    <select ng-model="yourSelect"         ng-options="option as option for option in ['var1', 'var2', 'var3']"        ng-init="yourSelect='var1'"></select>    <input type="hidden" name="yourSelect" value="{{yourSelect}}" /></form>

When you submit your form you can get value of input hidden.


DEMO

ng-selectedng-repeat


If you setup your select like the following:

<select ng-model="myselect" ng-options="b for b in options track by b"></select>

you will get:

<option value="var1">var1</option><option value="var2">var2</option><option value="var3">var3</option>

working fiddle: http://jsfiddle.net/x8kCZ/15/