2017-09-25 112 views
2

我對angular2應用程序,我有父組件彈出一個子組件角2重新渲染從@ViewChild組件

@ViewChild(childPopupComponent) case: childPopupComponent; 

我想要做的是工作父組件:從按下保存按鈕時子組件(在父組件內彈出),重新渲染父組件以進行反射(而不是重新加載整個頁面)。

我知道,這重新渲染的電流分量,但如何做到這一點從喜歡你的子組件的 @ViewChild一個

this.ref.detectChanges(); 

回答

1

進樣ChangeDetectorRef

export class childPopupComponent { 
    constructor(public cdRef: ChangeDetectorRef) {} 

和之後,您將只能在子組件上運行變更檢測循環:

@ViewChild(childPopupComponent) case: childPopupComponent; 

this.case.cdRef.detectChanges(); 

要更新家長可以在孩子注入它

export class childPopupComponent { 
    constructor(public parent: ParentComponent) {} 

然後

this.parent.cdRef.detectChanges() 

甚至嘗試這個辦法:

import { ChangeDetectorRef, SkipSelf } from '@angular/core'; 

export class childPopupComponent { 
    constructor(@SkipSelf() private parentCdRef: ChangeDetectorRef) {} 
+0

真的謝謝你,如果我想注入整個父組件內部的孩子,以便從孩子調用父功能..我怎麼能這樣做,因爲它會拋出奇怪的錯誤 無法解析casesPopupComponent的所有參數:([object Object],[object Object],[object Object],[object Object],[object Object],[object Object] ?? – sockeros

+0

'?'通常會發生具有循環依賴性。你可以提供抽象類來解決它https://stackoverflow.com/questions/43198098/angular2-dependency-injection-not-working-when-injected-into-a-dynamic-compone/43198716#43198716 – yurzui

+0

爲你的另一種解決方案在你的子組件和父模板中有@Ouput事件只是爲它定義' yurzui