Apparently I did not yet understand the mechanic behind ng-repeat, $$hashKeys and track by.
I am currently using AngularJS 1.6 in my project.
The Problem:
I got an array of complex objects which i want to use to render a list in my view. But to get the required result i need to modify (or map/enhance/change) these objects first:
const sourceArray = [{id: 1, name: 'Dave'}, {id:2, name: Steve}]
const persons = sourceArray.map((e) => ({enhancedName: e.name + e.id}))
//Thus the content of persons is:
//[{enhancedName: 'Dave_1'}, {enhancedName: 'Steve_2'}]
Binding this to the view should be working like this:
<div ng-repeat="person in ctrl.getPersons()">
{{person.enhancedName}}
</div>
However this obviously runs into a $digest()-loop, because .map returns new object-instances every time it is called. Since I bind this to ng-repeat via a function, it gets reevaluated in every $digest, the model does not stabilize and Angular keeps rerunning $digest-cycles because these objects are flagged as $dirty.
Why i am confused
Now this is not a new issue and there are several solutions for this:
In an Angular-Issue from 2012 Igor Minar himself suggested to set the $$hashKey-Property manually to tell angular that the generated objects are the same. This is his working fiddle, but since even this very trivial example still ran into a $digest-loop when i used it in my project, i tried upgrading the Angular-Version in the fiddle. For some reason it crashes.
Okay... since Angular 1.3 we have track by which should essentially solve this exact problem. However both
<div ng-repeat="person in ctrl.getPersons() track by $index">
and
<div ng-repeat="person in ctrl.getPersons() track by person.enhancedName">
crash with a $digest-loop. I was under the impression that the track by statement should let angular believe it works with the same objects, but apparently this is not the case since it just keeps checking them for changes. To be honest, i have no idea how i can properly debug the cause of this.
Question:
Is it possible to use a filtered/ modified array as data-source for ng-repeat?
I do not want to store the modified array on my controller, because i need to update its data constantly and would then have to maintain and refresh it manually in the controller instead of relying on databinding.