Shamoon Shamoon - 2 months ago 10
Javascript Question

ng-model isn't updating when I change a select option

I have a select box with some

ng-options
, but it's not updating the
ng-model
when I select an option. I have a fiddle up at http://jsfiddle.net/32tntojs/ to view the source.

My HTML is

<div ng-controller="MyCtrl">
PARENTID: {{ parent_id }}
<select ng-model="parent_id" ng-options="project.id as project.groupingName for project in projects track by $index"></select>
</div>


and when I select something, the
parent_id
doesn't update. What am I doing wrong?

Answer

Updated Fiddle : https://jsfiddle.net/32tntojs/3/

Your projects array of objects does not contain id so replaced it with gid

HTML :

<select ng-model="parent_id" ng-options="project.gid as project.groupingName for project in projects"></select>

Another mistake: as shouldn't be used with track by

Refer AngularJs Docs - https://docs.angularjs.org/api/ng/directive/ngOptions

Be careful when using select as and track by in the same expression

Comments