Set option selected angularjs
The generated html looks like this:. The value of each option element is the loop couter and the text inside the option element is the current array content. Note the label is the one generates the text inside each option element. So it should be same name as value if items array contains all premitive type.
In the above example, both label and value referred as item. The flexibility of ng-options is when the array contains objects rather than plain strings. The above code is pretty simple to understand. By default, unless specified, the model bound the parent select box will be the value of the expression. That means, in the previous example, the item is the model for the select element. If we select the second element in the select box, the model is this:.
However, AngularJS provides another variation of expression to change this behaviour. Using select as we can specify a different model to the select box. Here, each option will have the text as item. Group by is really a short cut for adding options groups. But here, we can specify ng-options to render optgroups based on group value.
The above expression tells Angular to group each options based on the item. If we run the code, the output looks like this. This is similar to the previous one, and the only addition is the track by.
The syntax and usage is same as with ng-repeat. For example, we can use like this:. Bennadel explained this pretty well in his blog post. The expression for object data source is similar to the array data source. The only difference in object data source is, everything is key value pairs. The generated html is a select box with all the key names. Similarly, we can create the select box with only values, shown below.
The remaining expressions available for object data source is given below. There is no need of any explanation as they are similar to the array data source.
The only difference is, for objects, both key, value are available. A brief walk-through of the ng-options in AngularJS https: Mastering it properly will save lot of time in your next project.