2

I am working on animations in Angular, and am running into an issue where the block will not fade out.
HTML in fadeblock:

<div class="fadeBlock mx-auto" [@changeState]="currentState"></div>

Component file:

import { Component, OnInit } from '@angular/core';
import { trigger, state, style, animate, transition, useAnimation } from 
'@angular/animations';

import { fadeAnimation } from '../animations';

@Component({
selector: 'app-fadeblock',
templateUrl: './fadeblock.component.html',
styleUrls: [ './fadeblock.component.scss' ],
 animations: [
trigger('changeState', [
  transition('void => *', [
      useAnimation(fadeAnimation, {
          params: {
              delay: '1000ms',
              from: 1,
              to: 0,
              time: '1s'
          }
      })
  ])
  ])
  ]
})
export class FadeblockComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}

and fadeAnimation:

import {
animation, trigger, animateChild, group,
transition, animate, style, query
} from '@angular/animations';


export const fadeAnimation = animation([
style({
    opacity: '{{ from }}'
}),
animate('{{ time }} {{ delay }} ease-in-out', style({
    opacity: '{{ to }}'
})),
]);

If I change the opacity in the css file, the block will just stay and not move. I get no errors when running ng serve, but do when I try to run ng build --prod Any assistance is greatly appreciated as I try to learn Angular.

Augustin R
  • 7,089
  • 3
  • 26
  • 54
Jim
  • 1,315
  • 4
  • 17
  • 45

1 Answers1

2

You need to define a currentState property in FadeblockComponent to change the animation state.

export class FadeblockComponent implements OnInit {

    currentState; // it is null since you set transition from void to *

    constructor() { }

    ngOnInit() {
       this.currentState = 'entered'; // or whatever the state you want since you set ending transition to *
    }

}

Here's a working stackblitz project: https://stackblitz.com/edit/angular-cjyblw

Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35