-
Notifications
You must be signed in to change notification settings - Fork 708
How do i use that directive with ngRepeat using track by? #96
Comments
track by So my recommendation is not to use track by at all and let angular take care of it. Why do you want to use it anyways? |
Using ngRepeat without track by causes a very well known preformance issue in angular, adding track by saves a lot of cpu power and enhances tge preformance. I will try to use the $$hashKey as I have no other constant field on that specific model The thing is that in that array i have models brought from the server containing ID and new items that weren't uploaded yet that don't have any id. edit: |
I don't believe this is true in general, as it automatically tracks by $$hashKey. You only get into performance problems if you do anything to the $$hashKey fields, e.g. replacing the entire list will generate new $$hashKeys and therefore rerender the whole DOM. Did you actually measure the performance of your application? How big is your list? |
The list containing the issue is small, so it's not such a big issue, the thing is that it is very recommended by the angular team to use track by on all ngRepeats as it improves performance significantly as you can see in almost every article concerning angularJS performance. for now i just removed the track by |
I think you're optimizing prematurely, but if you really want to use track by, just choose a field on your object that is unique, and make sure to update it in the dnd-drop callback, so that there are no duplicates. |
Well the problem is, the moment when you remove item from list and add it to a new place, to same objects ar in ng-repeate. I got the same issue, trakby on unique ID from Database, and still got duplicate error: Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: payment in payBillCtrl.payments | filter:search track by payment.id, Duplicate ............. |
I see the same issue when using
Between steps 2 and 3, there's a duplicate element in the list, which is why I imagine that leaving the original element in the list is important for how canceled drags are handled, so a fix might be a bit involved. |
I see the problem. One workaround would be to use dnd-drop to remove the old element / invalidate all elements with the same ID by setting the ID to null. I don't have any idea how to solve this cleanly right now. |
I have the similar issue but I track by a unique item ID property for my object (not $index). In my case some nested lists do see a performance impact by not using trackby. So unfortunately, I have to use the track by ... Is it possible to:
Could possibly also have the case that if user attribute track-by-id=0 then it means they are tracking by $index and all other cases it is simply user specified tracking property name. Would this work? |
I think that this is not just about performance. The directive is buggy sometimes when working with multiple lists and I'm pretty sure that is caused by its reliance on the default tracking by hash which is often unreliable. Some examples: Sometimes when working with 2 lists when dropping one item to the other overwrites some other item or when deleting an item sometimes another one in the wrong list is deleted. |
With the new dnd-callback you can implement move by reference like this: https://jsfiddle.net/Ldxffyod/1/ In that case there are no more duplicate items in the list, and tracking by a custom property should work without weird fake properties or hacks. Small caveat is that this does not work with dnd-external-sources, but I assume that's no problem for most people. |
Hi, whenever i use track by on my ngRepeat, especially "track by $index" it causes a lot of trouble, what is the best way to use it with track by?
The text was updated successfully, but these errors were encountered: