javascript - What is "track by" in AngularJS and how does it work? -


i don't understand how track by works , does.
main goal use ng-repeat add precision.

using track by track strings & duplicate values

normally ng-repeat tracks each item item itself. given array objs = [ 'one', 'one', 2, 'five', 'string', 'foo'], ng-repeat attempts track changes each obj in ng-repeat="obj in objs". problem have duplicate values , angular throw error. 1 way solve have angular track objects other means. strings, track $index solution haven't other means track string.

track by & triggering digest & input focuses

you allude fact you're new angular. digest way of saying changes manifest in angular based on changes occur through triggering actions. click button update model via ng-click, registers change in digest cycle. i'm not articulate in explaining should investigate further if didn't clarify things.

so track by. let's use example:

  1. call service return array of objects
  2. update object within array , save object
  3. after save service, depending on api returns, may:
    1. replace whole object or
    2. update value on existing object
  4. reflect change in ng-repeat ui

how track object determine how ui reflects change.

one of annoying uxs i've experienced this. have table of objects, each cell has input want in-line edit objects' properties. want change value, on-blur, save object while moving next cell edit while might waiting on response. autosave type thing. depending on how setup track by statement, may lose current focus (e.g. field you're editing) when response gets written array of objects.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -