简体   繁体   English

通过 ID 将 API 数据传递给 MatDialog

[英]pass API data to a MatDialog by ID

I have a list of API data but not all data is presented.我有一份 API 数据列表,但并未显示所有数据。 I try to get popUp via MatDialog to present more details which in that situation is "Comment".我尝试通过 MatDialog 弹出弹出窗口以呈现更多细节,在这种情况下是“评论”。 of course linked to the correct ReferennceNo/ column..Any suggetions?当然链接到正确的 ReferenceNo/ 列..任何建议?

ParentComponent.ts:父组件.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');
    });
  }

ParentComponent.html:父组件.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>

ChildComponent.ts: ChildComponent.ts:

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

  ngOnInit() {
 
  }

ChildComponent.html: ChildComponent.html:

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

Error: enter image description here错误:在此处输入图像描述

You pass the list as an argument to the dialog.您将列表作为参数传递给对话框。 But comment is not a property of the list and hence the error.但是comment不是列表的属性,因此是错误的。 You need to pass the element as an argument.您需要将元素作为参数传递。 Like this:像这样:

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');
    });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM