我有一個UserProfileComponent帶有專案串列的主要組件。附加到這個組件是一個HostListener用于我鍵盤上的導航鍵來瀏覽串列。
在選擇此串列中的一個專案時,我可以打開一個模式對話框,顯示有關此條目的更多資訊。
當模態對話框打開時,主組件中的 HostListener 仍然有效,我可以在后臺瀏覽串列。有沒有辦法將新元素傳遞給已經打開的模態對話框?
目前我只是在開始時傳遞專案,所以沒有像典型輸入那樣同步。
let dialogRef = dialog.open(UserProfileComponent, {
item: item, <--- item might change after the modal dialog is open
});
uj5u.com熱心網友回復:
似乎是一個可以使用 BehaviorSubject 的好場景。首先用一些值初始化它:
item = new BehaviorSubject<string>('default string');
現在,在呼叫模態對話框時:
let dialogRef = dialog.open(UserProfileComponent, {
item: item.asObservable(), <--- subscribe to this in the dialog
});
現在,每當您希望在對話框中傳遞新值時:
item.next('new string');
由于對話框組件訂閱了 observable,新值將被傳遞。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/348017.html
標籤:有角的
