Thursday, May 22, 2014

Angular.js: Track selected item by id not by reference

Using Angular.js I created a form where a user could select a value. The selected value is then bound to a property on the controller.

Here is the corresponding code:

The problem is that the moment I leave the screen and come back the dropdown no longer has an item selected. When searching through the Angular.js documentation I found the following important note:

image

When leaving the screen and coming back, the data is fetched again from the server, so the selected application object is no longer pointing to the same object as the application objects in the list.

To fix this you can use the ‘track by’ option inside the binding expression:

image

1 comment:

Raju Kumar said...

nice post and site, good work! This article is well written and quite informative. More articles should be written and you have just found a follower.and more visit

Angularjs Training In Hyderabad