Just create a simple material data table with one column f.e. and call a method in this column defenition like this:
<ng-container matColumnDef="test">
<th mat-header-cell *matHeaderCellDef> test</th>
<td mat-cell
*matCellDef="let element"> {{test()}} 掳</td>
</ng-container>
this calls the method test():
public test(): void {
console.log('test');
}
The expected behavior would be that the method test() is executed for each row in the table ONCE.
The current behavior is that the method test() is executed twice for each row in the table.
If the method call does complex compution the performance will drop by performing it twice for each row in the table.
I have transferred this issue to the correct repository for you.
If you need to do complex calculations, you should do them ahead of time, not by function calls through the template. This is a simple change detection effect.
That is how I solved it now. But isnt it strange change detection calls everything twice? Once should be enough?
Don't quote me on that but I think that in dev change detection runs twice to detect possible issues. Can you confirm that it also happens in production?
Also you should avoid binding functions in your template since these functions are executed at every change detection cycle.
Yes, in development mode change detection runs twice so that the famous "ExpressionHasChangedAfterItHasBeenCheckedError" can be thrown.
Angular itself makes no assurances about how many time functions within expressions will be called. You shouldn't ever depend on functions being called only a certain number of times by Angular's change detection engine.
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
_This action has been performed automatically by a bot._
Most helpful comment
I have transferred this issue to the correct repository for you.