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' ;
23import trace = require( "trace" ) ;
34
45type AnyComponentRef = ComponentRef < any > ;
@@ -22,12 +23,12 @@ function log(message: string) {
2223 template : `<Placeholder #loader></Placeholder>`
2324} )
2425export class DetachedLoader {
25- @ViewChild ( 'loader' , { read : ViewContainerRef } ) containerRef : ViewContainerRef ;
26+ @ViewChild ( 'loader' , { read : ViewContainerRef } ) childContainerRef : ViewContainerRef ;
2627
2728 private viewLoaded = false ;
2829 private pendingLoads : PendingLoadEntry [ ] = [ ] ;
2930
30- constructor ( private compiler : ComponentResolver , private changeDetector : ChangeDetectorRef ) { }
31+ constructor ( private compiler : ComponentResolver , private changeDetector : ChangeDetectorRef , private containerRef : ViewContainerRef ) { }
3132
3233 public ngAfterViewInit ( ) {
3334 log ( "DetachedLoader.ngAfterViewInit" ) ;
@@ -42,7 +43,7 @@ export class DetachedLoader {
4243
4344 private loadInLocation ( componentType : Type ) : Promise < ComponentRef < any > > {
4445 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 ) ;
4647 } ) . then ( ( compRef ) => {
4748 log ( "DetachedLoader.loadInLocation component loaded -> markForCheck" ) ;
4849 // Component is created, buit may not be checked if we are loading
@@ -66,7 +67,7 @@ export class DetachedLoader {
6667 // so that loading can conitionue.
6768 log ( "DetachedLoader.loadComponent -> markForCheck(with setTimeout())" )
6869 setTimeout ( ( ) => this . changeDetector . markForCheck ( ) , 0 ) ;
69-
70+
7071 return new Promise ( ( resolve , reject ) => {
7172 this . pendingLoads . push ( {
7273 componentType : componentType ,
@@ -75,4 +76,8 @@ export class DetachedLoader {
7576 } ) ;
7677 }
7778 }
79+
80+ public loadWithFactory < T > ( factory : ComponentFactory < T > ) : ComponentRef < T > {
81+ return this . containerRef . createComponent ( factory , this . containerRef . length , this . containerRef . parentInjector , null ) ;
82+ }
7883}
0 commit comments