2016-04-05 19 views
5

Angular2 Dinamik Bileşen Yükleyiciyi kullandım. Her şey iyi çalışıyor.Açısal 2 dinamik yüklü bileşen, olaylar geri gelmiyor mu?

Dinamik olarak yüklenen bileşenim olay yayar, ancak hiçbir yere yetişemiyorum.

Veli kodu:

this.dcl.loadAsRoot(SomeComponent, "#dynamiccomponenthere", this.injector) 

Veli şablonu:

<div id="dynamiccomponenthere" (somecustomevent)="someFunc()"></div> 

Çocuk:

... 
this.somecustomevent.emit(data) 
... 

* ÇÖZÜM: (thx Gunther) *

cmp.instance['somecustomevent'].subscribe(ev => { 
    this.consoleLog(ev) // run function in parent! 
}) 

cevap

6
  • LoadAsRoot Değişime anda loadAsRoot() yalnızca kök bileşen (AppComponent) bootstrap ve giriş kök bileşeni desteklenmez kullanılır

çağırmak değildir.

geçici çözüm, Çıkışlar, Enjektör değişimi algılama tetikleyebilir ve elle Girişleri kadar Tel gerekir https://github.com/angular/angular/issues/6370#issuecomment-193896657

loadAsRoot kullanarak bu yorumun ve bileşen atmayın fonksiyonu açıklanmıştır

function onYourComponentDispose() { 
} 
let el = this.elementRef 
let reuseInjectorOrCreateNewOne = this.injector; 
this.componentLoader.loadAsRoot(YourComponent, this.elementRef, reuseInjectorOrCreateNewOne, onYourComponentDispose) 
.then((compRef: ComponentRef) => { 
    // manually include Inputs 
    compRef.instance['myInputValue'] = {res: 'randomDataFromParent'}; 
    // manually include Outputs 
    compRef.instance['myOutputValue'].subscribe(this.parentObserver) 
    // trigger change detection 
    cmpRef.location.internalElement.parentView.changeDetector.ref.detectChanges() 
    // always return in a promise 
    return compRef 
}); 

Ayrıca bkz. @ContentChild is null for DynamicComponentLoader

+1

“myOutputValue” ile ilgili satıra bakın. Bu, loadAsRoot() 'ile eklenen bileşenlerin olayların dinlenmesinin nasıl yapıldığına bir örnektir. –

+0

Bu örnekte. This.parentObserver'nın ne olduğunu varsayalım? ParentObserver nereden geliyor? –

+1

Asla boşver .. Ben anladım. 'parentObserver' davamda dahili bir işlevdi .. IE 'private someFunction() {}' –

İlgili konular