I'm having an issue with fading in and out an error message while also using *ngIf. The fade-in works fine, but the fade out does not work. Instead, the text just disappears (as if set to display:none), even though the *ngIf does not take effect until the fade out is complete. I have tried converting to use string enumerations and numeric values but get the same effect. Is there an issue with my animations configuration?
Linked stackblitz is using Angular 10, I'm on Angular 11.0.7 (latest).
https://stackblitz.com/edit/angular-ivy-bsuupd?file=src/app/form-error.component.ts
@Component({
selector: "app-form-error",
animations: [
trigger("fadeInOut", [
state("false", style({ opacity: 0 })),
state("true", style({ opacity: 1 })),
transition("true <=> false", animate(500))
])
],
template: `
<p>showError is {{ showError }}</p>
<p>fadeError is {{ fadeError }}</p>
<ng-container *ngIf="showError">
<h1 [@fadeInOut]="{ value: fadeError }">{{ errorText }}</h1>
</ng-container>
`
})
export class FormErrorComponent implements OnInit, OnChanges {
@Input() errorText: string;
showError: boolean = false;
fadeError: boolean = false;
constructor() {}
ngOnInit(): void {}
ngOnChanges(changes: SimpleChanges): void {
if (this.errorText) {
this.showError = true;
setTimeout(() => {
// lets fade in begin after ngIf takes effect (works fine)
this.fadeError = true;
});
} else {
this.fadeError = false; // this should start fade, but just hides the text immediately
setTimeout(() => {
// delays the ngIf until fade out is done
this.showError = false;
}, 500);
}
}
}
question from:
https://stackoverflow.com/questions/65646139/angular-animations-ngif-opacity-only-works-in-one-direction 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…