Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
2 views
in Web Technology by (47.6k points)

Here is what seems to be bothering a lot of people (including me).

When using the ng-options directive in AngularJS to fill in the options for a <select> tag, I cannot figure out how to set the value for an option. The documentation for this is really unclear - at least for a simpleton like me.

I can set the text for an option easily like so:

ng-options="select p.text for p in resultOptions"

When resultOptions is for example:

   { 

      "value": 1, 

 "text": "1st" 

    },

    {

  "value": 2,

  "text": "2nd" 

     }

 ]

It should be (and probably is) the most simple thing to set the option values, but so far I just don't get it.

1 Answer

+2 votes
by (106k points)
edited by

For setting the value property in AngularJS' ng-options you can follow the below-mentioned steps:-

With track by an expression, you can set the actual value for the value attribute. Of select element. 

Are you interested in learning Angularjs from the basics! Here's the right video for you on Angularjs provided by Intellipaat:

See the code below:-

 <select ng-options="obj.text for obj in array track by obj.value"> 

</select>

Related questions

+2 votes
1 answer
+2 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

Browse Categories

...