Why the Angular ng-options value contains the value data type?
Asked Answered
S

2

7

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 ?

Sandhi answered 16/8, 2015 at 13:19 Comment(3)
could you create a plunkr of the same?Willner
Why do you care? You shouldn't mess with the DOM anyway. What matters is what your model (i.e. $scope.user and $scope.color) containsMavis
I'm reading it from Json array like [ { "id": 1, "name": "Ahmed" }, { "id": 2, "name": "Jon" } ]Sandhi
P
5

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"
Personify answered 6/2, 2017 at 12:11 Comment(0)
M
0

Right now there is not option in angular to remove that type. If you need that, use custom directive with ng-option as parent.

Marinna answered 16/8, 2015 at 13:40 Comment(2)
can you send to me example please :)Sandhi
May i know why u need that option, Is it must or not??Marinna

© 2022 - 2024 — McMap. All rights reserved.