6

I have created an animation for fading in/out an image when its src changes. When I quickly switch between images (before the animation is done), I get the following error:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. 
Previous value: 'src: http://localhost/wp-content/uploads/2018/11/billede1.jpg'. 
Current value: 'src: http://localhost/wp-content/uploads/2018/11/billede2.jpg'

Visually, there is nothing wrong though. The animations are working properly and the correct images are showing up - even when I switch between them quickly and the error gets printed. I would like to get rid of the error though. Any ideas about how I can do that?

Animation:

imageAnimation = trigger('imageAnimation', [
    state('in', style({opacity: 1, })),
    state('out', style({opacity: 0, })),
    transition('in <=> out', [
        animate('0.25s ease')
    ])
])

HTML:

  <div id="main-image-container" (mousemove)="displayBars()" [style.cursor]="mouseMovement ? 'default' : 'none'">
    <img id="main-image" src="{{displayedImage}}" [@imageAnimation]="imgState" (@imageAnimation.done)="onImageAnimationDone($event)">
  </div>
  <div id="bottom-bar">
    <div class="img-container" *ngFor="let img of gun.images">
        <img src="{{img.thumbnail_image_url}}" [class.selected]="img.full_image_url == selectedImage" (click)="setSelectedImage(img.full_image_url)">
    </div>
  </div>

Component:

  onImageAnimationDone(event: any){
    this.displayedImage = this.selectedImage;
    this.imgState = "in";
  }

  setSelectedImage(url: string){
    if(url != this.selectedImage){
      this.imgState = "out";
      this.selectedImage = url;
    }
  }

Stackblitz: https://stackblitz.com/edit/angular-hg13qq

Jesper
  • 2,644
  • 4
  • 30
  • 65
  • where do you set the initial value of the image source? maybe post the code to stackblitz or paste all as snippets here – mchl18 Nov 29 '18 at 01:22
  • also, try using `[src]="img.thumbnail_image_url"`, using interpolation with `{{...}}` iin tags can be avoided using brackets, see: https://angular.io/guide/cheatsheet – mchl18 Nov 29 '18 at 01:23
  • @mchl18 I have tried that, but the result is the same. Here is a stackblitz: https://stackblitz.com/edit/angular-hg13qq – Jesper Nov 29 '18 at 13:01
  • I see no error in the stackblitz ??? – mchl18 Nov 29 '18 at 17:42
  • @mchl18 Have you tried clicking on the buttons many times quickly and then looking in the Chrome developer tools console? – Jesper Nov 29 '18 at 20:18
  • 1
    @Jesper hey, did you solve the issue? I have identical one... – Mariusz.v7 Oct 04 '20 at 14:40

0 Answers0