11import {
2- ApplicationRef ,
3- ComponentFactoryResolver ,
42 ComponentRef ,
53 EventEmitter ,
64 Injectable ,
7- Injector ,
85 OnDestroy ,
9- NgZone
6+ NgZone ,
7+ ViewContainerRef ,
8+ Injector
109} from "@angular/core" ;
1110
1211import { NotificationContent , ToastContent , ActionableContent } from "./notification-content.interface" ;
@@ -30,18 +29,13 @@ export class NotificationService implements OnDestroy {
3029 public onClose : EventEmitter < any > = new EventEmitter ( ) ;
3130
3231 /**
33- * Constructs NotificationService.
34- *
35- * @param injector
36- * @param componentFactoryResolver
37- * @param applicationRef
32+ * Constructs Notification Service
3833 */
3934 constructor (
4035 protected injector : Injector ,
41- protected componentFactoryResolver : ComponentFactoryResolver ,
42- protected applicationRef : ApplicationRef ,
43- protected ngZone : NgZone ) {
44- }
36+ protected viewContainer : ViewContainerRef ,
37+ protected ngZone : NgZone
38+ ) { }
4539
4640 /**
4741 * Shows the notification based on the `notificationObj`.
@@ -77,14 +71,11 @@ export class NotificationService implements OnDestroy {
7771 notificationObj : NotificationContent | ToastContent | ActionableContent ,
7872 notificationComp = Notification
7973 ) {
80- const componentFactory = this . componentFactoryResolver . resolveComponentFactory ( notificationComp ) ;
81-
82- let notificationRef = componentFactory . create ( this . injector ) ;
83- notificationRef . instance . notificationObj = notificationObj as any ; // typescript isn't being very smart here, so we type to any
74+ const notificationRef = this . viewContainer . createComponent ( notificationComp ) ;
75+ notificationRef . instance . notificationObj = notificationObj ;
8476 this . notificationRefs . push ( notificationRef ) ;
8577
8678 this . onClose = notificationRef . instance . close ;
87- this . applicationRef . attachView ( notificationRef . hostView ) ;
8879
8980 if ( notificationObj . target ) {
9081 document . querySelector ( notificationObj . target ) . appendChild ( notificationRef . location . nativeElement ) ;
@@ -155,7 +146,6 @@ export class NotificationService implements OnDestroy {
155146 if ( notificationRef instanceof Notification ) {
156147 this . close ( notificationRef . componentRef ) ;
157148 } else {
158- this . applicationRef . detachView ( notificationRef . hostView ) ;
159149 notificationRef . destroy ( ) ;
160150 const index = this . notificationRefs . indexOf ( notificationRef ) ;
161151 if ( index !== - 1 ) {
@@ -216,13 +206,8 @@ export class NotificationService implements OnDestroy {
216206 *
217207 */
218208 ngOnDestroy ( ) {
219- if ( this . notificationRefs . length > 0 ) {
220- for ( let i = 0 ; i < this . notificationRefs . length ; i ++ ) {
221- let notificationRef = this . notificationRefs [ i ] ;
222- this . applicationRef . detachView ( notificationRef . hostView ) ;
223- notificationRef . destroy ( ) ;
224- }
225- this . notificationRefs . length = 0 ;
226- }
209+ this . notificationRefs . forEach ( ( ref ) => {
210+ ref . destroy ( ) ;
211+ } ) ;
227212 }
228213}
0 commit comments