1
- import { ComponentRef , ComponentFactory , ViewContainerRef , Component , Type , ComponentFactoryResolver , ChangeDetectorRef } from '@angular/core' ;
1
+ import { ComponentRef , ComponentFactory , ViewContainerRef , Component , Type , ComponentFactoryResolver , ChangeDetectorRef , ApplicationRef , OnDestroy } from '@angular/core' ;
2
2
import { Trace } from '@nativescript/core' ;
3
3
4
4
/**
@@ -10,13 +10,20 @@ import { Trace } from '@nativescript/core';
10
10
selector : 'DetachedContainer' ,
11
11
template : `<Placeholder #loader></Placeholder>` ,
12
12
} )
13
- export class DetachedLoader {
13
+ export class DetachedLoader implements OnDestroy {
14
+ private disposeFunctions : Array < ( ) => void > = [ ] ;
14
15
// tslint:disable-line:component-class-suffix
15
- constructor ( private resolver : ComponentFactoryResolver , private changeDetector : ChangeDetectorRef , private containerRef : ViewContainerRef ) { }
16
+ constructor ( private resolver : ComponentFactoryResolver , private changeDetector : ChangeDetectorRef , private containerRef : ViewContainerRef , private appRef : ApplicationRef ) { }
16
17
17
18
private loadInLocation ( componentType : Type < any > ) : Promise < ComponentRef < any > > {
18
19
const factory = this . resolver . resolveComponentFactory ( componentType ) ;
19
- const componentRef = this . containerRef . createComponent ( factory , this . containerRef . length , this . containerRef . injector ) ;
20
+ const componentRef = factory . create ( this . containerRef . injector ) ;
21
+ this . appRef . attachView ( componentRef . hostView ) ;
22
+
23
+ this . disposeFunctions . push ( ( ) => {
24
+ this . appRef . detachView ( componentRef . hostView ) ;
25
+ componentRef . destroy ( ) ;
26
+ } ) ;
20
27
21
28
// Component is created, built may not be checked if we are loading
22
29
// inside component with OnPush CD strategy. Mark us for check to be sure CD will reach us.
@@ -27,6 +34,10 @@ export class DetachedLoader {
27
34
return Promise . resolve ( componentRef ) ;
28
35
}
29
36
37
+ public ngOnDestroy ( ) {
38
+ this . disposeFunctions . forEach ( ( fn ) => fn ( ) ) ;
39
+ }
40
+
30
41
public detectChanges ( ) {
31
42
this . changeDetector . markForCheck ( ) ;
32
43
}
0 commit comments