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
Add a ment  | 

1 Answer 1

Reset to default 6

You 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