I am trying to achieve a simple animation with angular. On the click of the button I change the state of showState
from to shown
. Since I am using *ngIf I have used void
keyword in the animation and yet it is not working.
CSS
p {
border: 1px solid black;
background-color: lightblue;
padding: 10px;
}
app.component.ts
import { showStateTrigger } from './animations';
import { Component } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"],
animations: [
showStateTrigger
]
})
export class AppComponent {
isShown = false;
}
HTML
<button (click)="isShown = !isShown">Toggle Element</button>
<p [@showState]="isShown ? 'shown' : 'notShown'" *ngIf="isShown"> You can see me now!</p>
Animations.ts
import { state, style, transition, trigger, animate } from "@angular/animations";
export const showStateTrigger = trigger("showState", [
transition('void => shown', [
style({
opacity: 0
}),
animate(2000, style({
opacity: 1
}))
])
]);