Buckwheat Buckwheat - 6 months ago 9
Javascript Question

ng-options and ng-model preselected value

I'm trying to preselect value with ng-options. Here's what

<select ng-model="promocode.PRODUCT" ng-change="getSomething()" ng-options="product as product.NAME for product in products">


The problem is in selected by default value. I have
promocode.PRODUCT
object the same as in ng-options, but angular set default value empty.

Where is my mistake?

Answer

Assuming product has id property, You need to use select as label group by group for value in array track by trackexpr, Read DOCs

 <select ng-model="promocode.PRODUCT"
    ng-change="getSomething()"
    ng-options="product as product.NAME for product in products track by product.ID">
 </select>

jsFiddle