Skip to content

Is it by design that Angular does not notice array changes during the second check in development mode? #43187

Closed

Description

@aweebit
@Pipe({
  name: 'reverse',
  pure: false
})
export class ReversePipe implements PipeTransform {
  transform<T>(value: T[]): T[] {
    return value.reverse();
  }
}

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,

transform<T>(value: T[]): T[] {
  value.reverse();
  return [...value];
}

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?

Activity

  1. added
    area: core
    on Aug 18, 2021
  2. added this to the needsTriage milestone on Aug 18, 2021
  3. atscott commented on Aug 18, 2021

    @atscott
    Contributor

    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.

  4. added
    needs reproduction
    on Aug 18, 2021
  5. modified the milestones: needsTriage, Backlog on Aug 18, 2021
  6. aweebit commented on Aug 18, 2021

    @aweebit
    Author

    @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 *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.

  7. atscott commented on Aug 18, 2021

    @atscott
    Contributor

    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.

    Closing as a duplicate of #20450

  8. angular-automatic-lock-bot commented on Sep 18, 2021

    @angular-automatic-lock-bot

    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.

  9. locked and limited conversation to collaborators on Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: change detectionneeds reproduction

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions