Abaij
Abaij

Reputation: 873

How to put the right value to select option value generated by angularjs?

I made select field to show a list of products using angularjs. I need to grab the option value when the form submitted. But I found that the option values are wrong. I expected the values like 1,2,3 etc (whatever product ID in the data array), instead this is what I get:

<option label="Product A" value="object:1">Product A</option>
<option label="Product B" value="object:2">Product B</option>
<option label="Product C" value="object:3">Product C</option>

How to put the right value in this case?

Here is my code:

<select ng-options="item.product for item in data" ng-model="product" name="product" class="form-control" ng-change="selectColorByProduct(item.id_product)"></select>

Upvotes: 0

Views: 21

Answers (1)

Tarafder Ashek E Elahi
Tarafder Ashek E Elahi

Reputation: 292

You need to add item as item.product for label and track by in your ng-options for id. Something like:

<select ng-options="item as item.product for item in data track by item.id_product" ng-model="product" name="product" class="form-control" ng-change="selectColorByProduct(item.id_product)"></select>

Upvotes: 1

Related Questions