我需要幫助,因爲我現在有點迷路了。所以,我有一個動態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;
然而,這將只是未定義,不是我所需要的。如何將訂閱中的方向數據傳遞給子組件,或者我需要接收事件/方向字符串的代碼/功能?任何建議表示讚賞。
如果我的措辭不好或令人困惑,請說,我會重新提出這個問題。
如果用戶點擊瀏覽器後退按鈕,應用程序的行爲如何?你是否希望它與<按鈕(點擊)=「移動('後退')」>返回相同。或者你想讓它進入用戶訪問的最後一頁。如果你想讓它步進一個'對話框',你應該使用角度路由。前後移動可以是[routerLink]與具有不同url參數的同一頁面。 –
行爲並不重要,我正在瀏覽數據而不是窗口,我只想將字符串傳遞給孩子,一切都照顧好。 –
好的回答合起來 –