2

Here is what seems to be bothering a lot of people (including me). When using the ng-options directive in AngluarJS 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

When I use ng-options inside select tag like this =>

<select ng-options="cat as cat.name for cat in categories" ng-model="selectedCategory" name="category" class="form-control"></select>

I get the options like this =>

<option label="Mobile" value="object:5">Mobile</option>

instead I want it like this =>

<option value="Mobile">Mobile</option>

2 Answers 2

1

Use track by cat.name

<select ng-options="cat as cat.name for cat in categories track by cat.name" 
        ng-model="selectedCategory" name="category" class="form-control">
</select>

http://plnkr.co/edit/me0vis?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you for up votes. Feel free to ask, and good luck in learning! :) @ChadidiAbdellah
0

this should woork, you need to add "track by cat.name" like so :

<select ng-options="cat as cat.name for cat in categories track by cat.name" ng-model="selectedCategory" name="category" class="form-control"></select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.