TL;DR:
Angular raises ExpressionChangedAfterItHasBeenCheckedError
when using an iterator with ngFor
.
I have a template which uses ngFor
like:
<div *ngFor="let something of foobarIterator()">
The TypeScript code being like:
private foobar = [];
public foobarIterator() {
return this.foobar[Symbol.iterator]();
}
According to Angular documentation it is allowed to pass an iterator to ngForOf and it actually works… except it throws an ExpressionChangedAfterItHasBeenCheckedError
afterwards.
While I understand that it may be because I return a new instance of iterator every time the method is called, I do not get what Angular expects instead. Using the same instance of the iterator after it has been exhausted makes no sense – it's an iterator after all, not the array itself.
What is the correct way of using iterators with ngFor
then?