簡體   English   中英

在 mat-dialog angular 中傳遞動態 object 鍵

[英]Pass dynamic object key in mat-dialog angular

我正在使用 angular 材料的 mat-dialog。 這是我的代碼:

if(abc_name === name){
    const dialogRef = this.dialog.open(ViewAbcComponent, {
      panelClass: 'full-screen-dialog-container',
      data: {abc_name: this.abcDetails.item1[keyValue]}
    });
    dialogRef.afterClosed().subscribe((result) => {
      if(result){}
      console.log("ViewAbcComponent dialog closed", result);
    });
  }

data: object 我想要 tp 傳遞動態abc_name值。 知道如何實現嗎?

您可以在服務中創建主題/行為主題,並在您的 ViewAbcComponent 組件中收聽(訂閱)該主題。 一旦您的數據發生更改,您必須將數據更新為subject.next(data); 你會在組件中得到它。 主題示例:

const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));

行為主體示例:

const subject = new Rx.BehaviorSubject(0);
subject.next(1);
subject.subscribe(x => console.log(x));

詳細說明:(根據要求)

1.您必須在服務中添加變量,比如說app.service.ts並添加更新方法。

const subject = new Rx.Subject();

  updateSubject(data){
   subject.next(data)
  } 
  1. 無論您的數據在哪個組件中更改,您都必須調用此 updateSubject 方法來更新數據。 像這樣this.appService.updateSubject(updatedData);

  2. ViewAbcComponent組件中,您已經訂閱了它

    this.appService.subject.subscribe(x => console.log("mydata",x));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM