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:
- call service return array of objects
- update object within array , save object
- after save service, depending on api returns, may:
- replace whole object or
- update value on existing object
- 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
Post a Comment