Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-options with simple array init

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.

script.js

$scope.options = ['var1', 'var2', 'var3']; 

html

<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.)

Edit:

My form is submitted externally, which is why I need 'var1' as the value instead of 0.

like image 238
Dragu Avatar asked Aug 13 '13 06:08

Dragu


People also ask

How do I set default Ng-options?

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.

Which ng directive is used to initialize?

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.

What are ng-options?

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.

How do ng-options work?

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.


2 Answers

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.

like image 154
James Davies Avatar answered Sep 21 '22 22:09

James Davies


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

like image 41
EpokK Avatar answered Sep 18 '22 22:09

EpokK