admin管理员组文章数量:1395886
I have a Notification ponent that's used to show notifications at the top of the screen. I want to have these notifications fade in and out. The NotificationService
has an array of notifications. When a new notification is added, a timer is set via setTimeout
that will remove the notification after 5 seconds.
The notifications appear and disappear correctly, but the fade animation is only working on the :enter
transition, when the notification appears. When the notification is removed, it simply disappears without a fade animation.
Any idea what I'm doing wrong?
notificationponent.ts
:
animations: [
trigger('fade', [
transition(':enter', [style({ opacity: 0 }), animate('0.2s', style({ opacity: 1 }))]),
transition(':leave', [style({ opacity: 1 }), animate('0.2s', style({ opacity: 0 }))])
])
]
notificationponent.html
:
<div @fade class="notification notification-{{ notification.theme }}">
<div class="icon"><fa-icon [icon]="icons[notification.theme]"></fa-icon></div>
<div class="message">{{ notification.message }}</div>
<div class="close"><fa-icon (click)="closeButtonClicked()" [icon]="icons.close"></fa-icon></div>
</div>
appponent.html
:
<div id="notification-container">
<app-notification *ngFor="let notification of notifications" [notification]="notification"></app-notification>
</div>
appponent.ts
:
get notifications() {
return this.notificationService.notifications;
}
notification.service.ts
:
export class NotificationService {
notifications: Notification[] = [];
showNotification(notificationToShow: Notification) {
this.notifications = [notificationToShow, ...this.notifications];
setTimeout(() => this.removeNotification(notificationToShow), 5000);
}
removeNotification(notificationToRemove: Notification) {
this.notifications = this.notifications.filter(notification => notification !== notificationToRemove);
}
}
I have a Notification ponent that's used to show notifications at the top of the screen. I want to have these notifications fade in and out. The NotificationService
has an array of notifications. When a new notification is added, a timer is set via setTimeout
that will remove the notification after 5 seconds.
The notifications appear and disappear correctly, but the fade animation is only working on the :enter
transition, when the notification appears. When the notification is removed, it simply disappears without a fade animation.
Any idea what I'm doing wrong?
notification.ponent.ts
:
animations: [
trigger('fade', [
transition(':enter', [style({ opacity: 0 }), animate('0.2s', style({ opacity: 1 }))]),
transition(':leave', [style({ opacity: 1 }), animate('0.2s', style({ opacity: 0 }))])
])
]
notification.ponent.html
:
<div @fade class="notification notification-{{ notification.theme }}">
<div class="icon"><fa-icon [icon]="icons[notification.theme]"></fa-icon></div>
<div class="message">{{ notification.message }}</div>
<div class="close"><fa-icon (click)="closeButtonClicked()" [icon]="icons.close"></fa-icon></div>
</div>
app.ponent.html
:
<div id="notification-container">
<app-notification *ngFor="let notification of notifications" [notification]="notification"></app-notification>
</div>
app.ponent.ts
:
get notifications() {
return this.notificationService.notifications;
}
notification.service.ts
:
export class NotificationService {
notifications: Notification[] = [];
showNotification(notificationToShow: Notification) {
this.notifications = [notificationToShow, ...this.notifications];
setTimeout(() => this.removeNotification(notificationToShow), 5000);
}
removeNotification(notificationToRemove: Notification) {
this.notifications = this.notifications.filter(notification => notification !== notificationToRemove);
}
}
Share
Improve this question
asked Jun 30, 2018 at 20:16
Joe AttardiJoe Attardi
4,5213 gold badges41 silver badges43 bronze badges
1 Answer
Reset to default 6You should put the @fade
on the parent element (<app-notification>
).
That element is responsible for creating / destroying each notification and as it doesn’t know about the animation on its child, it just removes it before any animation can happen.
本文标签: javascriptLeave animation not working in Angular 6 componentStack Overflow
版权声明:本文标题:javascript - Leave animation not working in Angular 6 component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744090639a2589355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论