Explain differ services for tracking custom object and list mutations.
01
01
Understand the problem
performanceadvanced
02
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Stuck? AI Nudge Available
Get a conceptual hint to guide your logic without spoiling the final implementation.
03
03
Study the solution
The solution is waiting
Give it an honest attempt first — then compare your thinking with the full walkthrough.
04
04
Read the code
Building a custom list change tracking directive using IterableDiffers
import { Directive, Input, DoCheck, IterableDiffers, IterableDiffer, inject } from '@angular/core';
@Directive({
selector: '[appTrackChanges]',
standalone: true
})
export class TrackChangesDirective implements DoCheck {
@Input() appTrackChanges!: string[];
private differs = inject(IterableDiffers);
private differ: IterableDiffer<string> | null = null;
ngDoCheck() {
if (this.appTrackChanges && !this.differ) {
this.differ = this.differs.find(this.appTrackChanges).create();
}
if (this.differ) {
const changes = this.differ.diff(this.appTrackChanges);
if (changes) {
changes.forEachAddedItem(record => {
console.log('Item added:', record.item, 'at index:', record.currentIndex);
});
changes.forEachRemovedItem(record => {
console.log('Item removed:', record.item, 'from index:', record.previousIndex);
});
}
}
}
}05
05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.