【问题标题】:pass API data to a MatDialog by ID通过 ID 将 API 数据传递给 MatDialog
【发布时间】:2021-04-09 05:05:29
【问题描述】:

我有一个 API 数据列表,但并未显示所有数据。我尝试通过 MatDialog 弹出弹出窗口以呈现更多细节,在这种情况下是“评论”。当然链接到正确的 ReferenceNo/ 列。有任何建议吗?

父组件.ts:

  constructor(private service:NilexService , public dialog: MatDialog  )  { }

  TicketsList:any=[];

 ngOnInit(): void {
     
this.refreshTicList();
 }

refreshTicList(){
    this.service.getAllTicList().subscribe(data=>{
      this.TicketsList =data as string[];
      this.TicketsList = new MatTableDataSource(data);
      this.TicketsList.sort = this.sort;
    this.TicketsList.paginator = this.paginator;
    this.TicketsList.tooltrip = this.tooltrip;
      
    });
  }

openDialog(element: any): void {
    

    let dialogRef = this.dialog.open(ChildComponent, {
      width: '1720px',
      height: '500px',
      panelClass: 'my-centered-dialog',
      data :{ element : this.TicketsList
      }
    });
    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
    });
  }

父组件.html:

 <ng-container class="example-button-container" matColumnDef="actions">
      <th mat-header-cell *matHeaderCellDef> actions </th>
        <td  mat-cell *matCellDef="let element">
          <button mat-icon-button (click)="openDialog(element)" >
          <mat-icon  color= "primary" >open_in_new</mat-icon> 
        </button>
      </td>

子组件.ts:

 constructor(
    public dialog_ref: MatDialogRef<ShowTicComponent>,
        @Inject( MAT_DIALOG_DATA ) public data: any) {
        
  }

  ngOnInit() {
 
  }

子组件.html:

 <div>
 {{data.TicketsList.comment}}
 </div>

错误: enter image description here

【问题讨论】:

    标签: angular api mat-dialog


    【解决方案1】:

    您将列表作为参数传递给对话框。但是comment 不是列表的属性,因此是错误的。您需要将元素作为参数传递。像这样:

    openDialog(element: any): void {
        let dialogRef = this.dialog.open(ChildComponent, {
          width: '1720px',
          height: '500px',
          panelClass: 'my-centered-dialog',
          data: { element } // <== this is it
        });
        dialogRef.afterClosed().subscribe(result => {
          console.log('The dialog was closed');
        });
      }
    

    【讨论】:

    • 我可以看到我从 consoleLog 上的元素获取数据,但 matdialog 仍然是空的
    • 您也需要更改对话框模板:{{data.element.comment}}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2012-04-07
    相关资源
    最近更新 更多