1
- import { ComponentRef , ViewContainerRef , Component , Type , ViewChild , ComponentResolver , ChangeDetectorRef , Host } from '@angular/core' ;
1
+ import { ComponentRef , ComponentFactory , ViewContainerRef , Component ,
2
+ Type , ViewChild , ComponentResolver , ChangeDetectorRef , Host } from '@angular/core' ;
2
3
import trace = require( "trace" ) ;
3
4
4
5
type AnyComponentRef = ComponentRef < any > ;
@@ -22,12 +23,12 @@ function log(message: string) {
22
23
template : `<Placeholder #loader></Placeholder>`
23
24
} )
24
25
export class DetachedLoader {
25
- @ViewChild ( 'loader' , { read : ViewContainerRef } ) containerRef : ViewContainerRef ;
26
+ @ViewChild ( 'loader' , { read : ViewContainerRef } ) childContainerRef : ViewContainerRef ;
26
27
27
28
private viewLoaded = false ;
28
29
private pendingLoads : PendingLoadEntry [ ] = [ ] ;
29
30
30
- constructor ( private compiler : ComponentResolver , private changeDetector : ChangeDetectorRef ) { }
31
+ constructor ( private compiler : ComponentResolver , private changeDetector : ChangeDetectorRef , private containerRef : ViewContainerRef ) { }
31
32
32
33
public ngAfterViewInit ( ) {
33
34
log ( "DetachedLoader.ngAfterViewInit" ) ;
@@ -42,7 +43,7 @@ export class DetachedLoader {
42
43
43
44
private loadInLocation ( componentType : Type ) : Promise < ComponentRef < any > > {
44
45
return this . compiler . resolveComponent ( componentType ) . then ( ( componentFactory ) => {
45
- return this . containerRef . createComponent ( componentFactory , this . containerRef . length , this . containerRef . parentInjector , null ) ;
46
+ return this . childContainerRef . createComponent ( componentFactory , this . childContainerRef . length , this . childContainerRef . parentInjector , null ) ;
46
47
} ) . then ( ( compRef ) => {
47
48
log ( "DetachedLoader.loadInLocation component loaded -> markForCheck" ) ;
48
49
// Component is created, buit may not be checked if we are loading
@@ -66,7 +67,7 @@ export class DetachedLoader {
66
67
// so that loading can conitionue.
67
68
log ( "DetachedLoader.loadComponent -> markForCheck(with setTimeout())" )
68
69
setTimeout ( ( ) => this . changeDetector . markForCheck ( ) , 0 ) ;
69
-
70
+
70
71
return new Promise ( ( resolve , reject ) => {
71
72
this . pendingLoads . push ( {
72
73
componentType : componentType ,
@@ -75,4 +76,8 @@ export class DetachedLoader {
75
76
} ) ;
76
77
}
77
78
}
79
+
80
+ public loadWithFactory < T > ( factory : ComponentFactory < T > ) : ComponentRef < T > {
81
+ return this . containerRef . createComponent ( factory , this . containerRef . length , this . containerRef . parentInjector , null ) ;
82
+ }
78
83
}
0 commit comments