You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is it by design that Angular does not notice array changes during the second check in development mode? #43187
This is how an unexperienced or forgetful developer could try to implement a pipe that reverses arrays. This naive implementation is wrong because reverse() reverses arrays in-place, so I would expect an ExpressionChangedAfterItHasBeenChecked error to be thrown in development mode. But not only is there no such error, the app even seems to work completely fine. The reason is that the array is reversed one more time during the second check, which brings it in its original order. But we cannot see it reflected in the UI because the rendering has already been completed at the time of the second check.
What is even more interesting is that there is no error even if we return a copy of the array we have just reversed,
so the problem has not only to do with the fact that the array reference does not change.
In both cases, the problem first becomes apparent when the app is built for production, meaning that the second check hides the problem instead of reporting it. Does not that contradict the purpose of the second check? Is it desired that it works this way?
The reason you don't get an error with the return value.reverse(); also depends on how exactly this is getting used. Direct uses of the array in the template, [input]="array"/{{ array }}, the difference cannot be detected because we're comparing two references to the same array. If you were to bind {{ array[0] }} then this would produce an error because the value in the template changes.
My original case included the *ngFor directive, which seems to combine individually accessing elements and using the array directly. But I guess that only the arrays are compared when *ngFor is used.
Right, Angular does not compare objects at all (code reference). After that, what ngFor does with the new input is kind of a different story. After that, the ngFor directive just assumes that the input rightly changed and it's supposed to do an update. The point where this change would need to be caught for the ExpressionChanged error was at the ngForOf input.
Activity
atscott commentedon Aug 18, 2021
Can you provide a reproduction of the issue where you don't get an error when you return a copy? This doesn't appear to be the case: https://stackblitz.com/edit/angular-ivy-kcuyfl?file=src%2Fapp%2Fapp.component.html
The reason you don't get an error with the
return value.reverse();also depends on how exactly this is getting used. Direct uses of the array in the template,[input]="array"/{{ array }}, the difference cannot be detected because we're comparing two references to the same array. If you were to bind{{ array[0] }}then this would produce an error because the value in the template changes.aweebit commentedon Aug 18, 2021
@atscott okay, it actually seems to work fine if the array elements are primitives. But in my case, they were objects. Here is an example: https://stackblitz.com/edit/angular-ivy-vuawty?file=src/app/app.component.ts
My original case included the
*ngFordirective, which seems to combine individually accessing elements and using the array directly. But I guess that only the arrays are compared when*ngForis used.atscott commentedon Aug 18, 2021
Right, Angular does not compare objects at all (code reference). After that, what
ngFordoes with the new input is kind of a different story. After that, thengFordirective just assumes that the input rightly changed and it's supposed to do an update. The point where this change would need to be caught for theExpressionChangederror was at thengForOfinput.Closing as a duplicate of #20450
angular-automatic-lock-bot commentedon Sep 18, 2021
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.