Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ng-selected work?

Tags:

Here is a snippet. Q2 is selected as I expect.

<select name="quarter" ng-model="Quarter" >     <option value="1" >Q1</option>     <option value="2" ng-selected="Quarter=='Q1'">Q2</option>     <option value="3">Q3</option>     <option value="4">4</option> </select> 

Changing 'Q1' to 'Q2' makes nothing as selected as I expect. Now putting ng-selected="Quarter=='Q1'" DOES NOT make Q1 selected until i delete ng-selected="Quarter=='Q2"

wtf. How is this suppose to work?

like image 215
BruteCode Avatar asked Apr 02 '13 18:04

BruteCode


People also ask

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.

What is ngInit?

The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.

How do I filter with Ng-options?

In AngularJS when you are using ng-options, you can filter out the options by calling a custom function you have in the controller. Let's say you have following set of employees and when you display all these employees inside HTML select using ng-options, you can see all the employees in a dropdown.


2 Answers

If you put the ng-selected on the option element, your option will be selected when the ng-selected value is true. In your case, the option Q2 is selected when Quarter is equal to Q1.

If you want to select the value passed in Quarter, you must put the ng-selected on the select element :

<select name="quarter" ng-model="Quarter" ng-selected="Quarter"         ng-options="Quarter for Quarter in Quarters" >     {{Quarter}} </select> 

Take a look at the select directive documentation.

like image 174
Bastien Caudan Avatar answered Sep 19 '22 09:09

Bastien Caudan


<select ng-model="hour">     <option ng-selected="hour == $index" ng-repeat="h in (((b=[]).length=24)&&b) track by $index" ng-bind="$index">{{h}}</option> </select> 

if you want a select for 24 hour, you can do this.

like image 31
user1927627 Avatar answered Sep 18 '22 09:09

user1927627