I'm a little bit confused with Angular and ng-options
.
I have a simple array and I want to init a select with it. But, I want that options value = label.
$scope.options = ['var1', 'var2', 'var3'];
<select ng-model="myselect" ng-options="o for o in options"></select>
What I get:
<option value="0">var1</option> <option value="1">var2</option> <option value="2">var3</option>
What I want:
<option value="var1">var1</option> <option value="var2">var2</option> <option value="var3">var3</option>
So I tried:
<select ng-model="myselect2" ng-init=0 ng-options="options[k] as v for (k,v) in options"></select> <select ng-model="myselect3" ng-init=0 ng-options="b as b for b in options"></select>
(But it didn’t work.)
My form is submitted externally, which is why I need 'var1' as the value instead of 0.
In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options , angular does the rest. Essentially when you define the $scope property your select will bind to assign it the default value from your data array.
The ng-init Directive is used to initialize AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application.
The ng-options Directive in AngularJS is used to build and bind HTML elements with options to a model property. It is used to specify <options> in a <select> list. It is designed specifically to populate the items on a dropdown list. This directive implements an array, in order to fill the dropdown list.
Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.
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-selected ng-repeat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With