Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the Angular ng-options value contains the value data type?

Guys after I used the angular directive ng-options I got this result

<select name="users" id="users" ng-options="user.id as user.name for user in users" ng-model="user">
    <option value="number:1" label="Developers">Ahmed</option>
    <option value="number:2" label="Designers">Jon</option>
    <option value="number:3" label="HR">Astm</option>
    <option value="number:4" label="Doctor">Fady</option>
</select>


<select name="colors" id="colors" ng-options="color.code as color.name for color in colors" ng-model="color">
    <option value="string:ff0000" label="Developers">Red</option>
    <option value="string:ffffff" label="Designers">White</option>
    <option value="string:000000" label="HR">Black</option>
</select>

why the option value contains the data type such as number:1 or string:ffffff ??

how I can remove the data type from it and keeping only the value ?

like image 716
Astm Avatar asked Aug 16 '15 13:08

Astm


People also ask

What does ng value do?

The ng-value directive sets the value attribute of a input element, or a select element.

What is difference between ng-repeat and Ng-options?

ng-repeat creates a new scope for each iteration so will not perform as well as ng-options. For small lists, it will not matter, but larger lists should use ng-options. Apart from that, It provides lot of flexibility in specifying iterator and offers performance benefits over ng-repeat.

How do I set default selected value in ng-options?

Use ng-init to set default value for ng-options . Save this answer.

What is Ng option AngularJS?

AngularJS ng-options Directive 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.


1 Answers

Add track by [id] at the end of ng-options.

In your case it would be:

ng-options="user.id as user.name for user in users track by user.id"

and

ng-options="color.code as color.name for color in colors track by color.code"
like image 122
Davor Avatar answered Oct 23 '22 05:10

Davor