2017-09-29 36 views
0

我需要幫助,因爲我現在有點迷路了。所以,我有一個動態injets子組件爲使用componentFactoryResolver其模板的組成部分,這是我的HTML將可見數據從父組件傳遞到使用componentFactoryResolver創建的Angular 2+中的子組件

<div class="dialog"> 
    <div #container></div> 
    <button (click)="move('back')">Back</button> 
    <button (click)="move('forwards')">Forwards</button> 
</div> 

也是在我的組件我有一個觀察的捕獲像這樣的按鈕的點擊,這裏是我的(編輯/縮小)代碼

// parent-component.ts 

@ViewChild('container', {read: ViewContainerRef}) 
public dialogContainer: ViewContainerRef; 

public navigationClick$: Observable<string> = new Subject<string>(); 

// click event on buttons 
public move(direction): void { 
    this.navigationClick$.next(direction); 
} 

// code to inject child dynamic component, please ignore the args/variables 

const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.data.component); 
this.componentRef = this.dialogContainer.createComponent(componentFactory); 
this.embeddedcomponent = this.componentRef.instance as IWizardDialog; 
this.embeddedcomponent.data = this.data.data; 

現在我想從navigationClick $傳遞最新觀察到的值的子組件我修訂因而我的父母組件的代碼...

const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.data.component); 
this.componentRef = this.dialogContainer.createComponent(componentFactory); 

// NEW CODE 
// here I subscribe to the observable and try to pass the value to the child component 
this.navigationClick$.subscribe((data: string) => { 
    this.componentRef.instance.direction = data; 
}); 

this.embeddedcomponent = this.componentRef.instance as IWizardDialog; 
this.embeddedcomponent.data = this.data.data; 

的訂閱父是工作,我會但我不確定我會如何捕捉期望/傳遞訂閱數據的子組件,例如可我只是把這個聲明爲一個Input()

// child-component.ts 

@Input() public direction: string; 

然而,這將只是未定義,不是我所需要的。如何將訂閱中的方向數據傳遞給子組件,或者我需要接收事件/方向字符串的代碼/功能?任何建議表示讚賞。

如果我的措辭不好或令人困惑,請說,我會重新提出這個問題。

+0

如果用戶點擊瀏覽器後退按鈕,應用程序的行爲如何?你是否希望它與<按鈕(點擊)=「移動('後退')」>返回相同。或者你想讓它進入用戶訪問的最後一頁。如果你想讓它步進一個'對話框',你應該使用角度路由。前後移動可以是[routerLink]與具有不同url參數的同一頁面。 –

+0

行爲並不重要,我正在瀏覽數據而不是窗口,我只想將字符串傳遞給孩子,一切都照顧好。 –

+0

好的回答合起來 –

回答

1

我會使用一項服務。不是ViewChild。隨着服務的組件不需要父知道對方

@Injectable() 
    export class YourService { 
    move$: Observable<any>; 
    private moveSubject: Subject<any> = new Subject(); 

    constructor() { 
    this.move$ = this.moveSubject.asObservable(); 
    } 

    public move(direction) { 
    this.moveSubject.next(direction); 
    } 


} 

用法

contructor(public yourService:YourService){ 
} 

HTML在父母在孩子

YourChild implements OnInit, OnDestroy { 
    private subscriptions: Subscription = new Subscription(); 
    constructor(private yourService:YourService) { 

    } 
    ngOnInit(): void { 
     this.subscriptions.add(this.yourService.move$.subscribe(direction => { 
      //Do something 
     }));   
    } 
    ngOnDestroy(): void { 
    this.subscriptions.unsubscribe(); 
} 

<button (click)="yourService.move('back')">Back</button> 

用法}

+0

非常感謝,但是'this.yourService.move $ .subscribe'似乎沒有拿到'下一個'被觸發,儘管我可以在服務本身中看到這一點,所以''。 next()'正在工作...我會繼續嘗試 –

+0

也許它的返回。在組件onButtonClick中添加一個方法,然後從那裏調用move?你使用什麼版本的Angular? –

+0

謝謝,我已經這樣做了...我會繼續擺弄並嘗試做這項工作...我將設置一個plunker/jsbin –

相關問題