@@ -3,10 +3,13 @@ import {
3
3
ChangeDetectionStrategy ,
4
4
ChangeDetectorRef ,
5
5
Component ,
6
+ ComponentFactoryResolver ,
6
7
ElementRef ,
7
8
Injector ,
8
9
OnDestroy ,
9
- OnInit
10
+ OnInit ,
11
+ ViewChild ,
12
+ ViewContainerRef
10
13
} from '@angular/core' ;
11
14
import { Observable , Subject } from 'rxjs' ;
12
15
import { startWith , takeUntil , tap } from 'rxjs/operators' ;
@@ -22,6 +25,7 @@ import { Bus, cssClass, toCss } from './utils';
22
25
changeDetection : ChangeDetectionStrategy . OnPush
23
26
} )
24
27
export class ToppyComponent implements OnInit , AfterViewInit , OnDestroy {
28
+ @ViewChild ( 'compOutlet' , { read : ViewContainerRef } ) compOutlet : ViewContainerRef ;
25
29
content : Content = {
26
30
type : ContentType . STRING ,
27
31
data : '' ,
@@ -33,9 +37,18 @@ export class ToppyComponent implements OnInit, AfterViewInit, OnDestroy {
33
37
el : HTMLElement | any ;
34
38
wrapperEl : HTMLElement | any ;
35
39
extra : string ;
40
+ pinj : any ;
41
+ compInstance ;
36
42
private die : Subject < 1 > = new Subject ( ) ;
37
43
38
- constructor ( private inj : Injector , private cd : ChangeDetectorRef , private elRef : ElementRef ) { }
44
+ constructor (
45
+ public inj : Injector ,
46
+ private cd : ChangeDetectorRef ,
47
+ private compResolver : ComponentFactoryResolver ,
48
+ private elRef : ElementRef
49
+ ) {
50
+ this . pinj = Injector ;
51
+ }
39
52
40
53
ngOnInit ( ) {
41
54
this . el = this . elRef . nativeElement ;
@@ -47,13 +60,23 @@ export class ToppyComponent implements OnInit, AfterViewInit, OnDestroy {
47
60
this . el . setAttribute ( 'data-tid' , this . tid ) ;
48
61
cssClass ( 'add' , cls , `[data-tid='${ [ this . tid ] } ']` ) ;
49
62
cssClass ( 'add' , [ this . config . bodyClass ] ) ;
50
- if ( this . content . type === ContentType . COMPONENT ) {
51
- Object . assign ( this . content . data [ 'prototype' ] , this . content . props ) ;
52
- }
53
63
}
54
64
55
65
ngAfterViewInit ( ) {
56
66
this . listenPos ( ) . subscribe ( ) ;
67
+ if ( this . content . type === ContentType . COMPONENT ) {
68
+ this . compInstance = this . setComponent ( this . content . props ) ;
69
+ Bus . send ( this . tid , 't_compins' , this . compInstance ) ;
70
+ }
71
+ }
72
+
73
+ setComponent ( props ) {
74
+ const compRef = this . compOutlet . createComponent (
75
+ this . compResolver . resolveComponentFactory ( this . content . data as any )
76
+ ) ;
77
+ Object . assign ( compRef . instance , props ) ;
78
+ compRef . changeDetectorRef . detectChanges ( ) ;
79
+ return compRef . instance ;
57
80
}
58
81
59
82
updateTextContent ( data : string ) : void {
0 commit comments