简体   繁体   English

Angular 4材质对话框将对象数组传递给对话框

[英]Angular 4 material dialog box passing in array of object to dialog box

Hi I am trying to pass an array of object to material dialog. 嗨,我正在尝试将对象数组传递给材质对话框。 Here is how i am doing it. 这就是我的做法。

Model
export interface IPoducts{
     recordname: string;
     comments: [{
         comment: string
      }]
}

In component class has following relevant 在组件类中有以下相关内容

        constructor(public dialog: MdDialog, private productService: ProductService){}
        prod: IProducts[]=[]
        ngOnInit(): void{  
    //service getting data from database       

this.productService.getProcessnotes().subscribe(producsts=>this.products=products,error=>this.errorMessage=<any>error);

         //opening dialgue
        openDialog(prod:any): void {
            let dialogRef = this.dialog.open(prodDialog, {
              width: '400px',
              height: '500px;',
            data: this.prod  <------------------passing the object array
              }); 
          }}

Dialogbox component. 对话框组件。

 @Component({
      selector: proddialog',
      templateUrl: 'dialogdetails.html',
    })
     export class ProdDialog implements OnInit{
           public pnote: IProducts[];
          constructor(
            public dialogRef: MdDialogRef<DialogOverviewExampleDialog>,
        @Inject(MD_DIALOG_DATA) public  data: {pnote:this.prod }) { }
         public pnote: products;
          onNoClick(): void {
            this.dialogRef.close();
          }
           public ngOnInit():void{
               this.pnote=this.data.prod;

           }

In main template button triggers the dialog box by passing a pnote from *ngFor="let pnote of products"

 <button md-raised-button (click)="openDialog(pnote)">Open Dialog</button>

Dialog box is 对话框是

<div>
  <h2 md-dialog-title>MY DIALOG</h2>
  <hr>
  <md-dialog-content>
        <div*ngFor=prod in products>
          {{prod.recorname}}
        </div>
    <br>
    <br>
    <strong>{{data}}</strong>
  </md-dialog-content>
  <hr>
  <md-dialog-actions>
    <button md-raised-button (click)="onCloseConfirm()">CONFIRM</button>&nbsp;
    <button md-raised-button (click)="onCloseCancel()">CANCEL</button>
  </md-dialog-actions>
</div>

When I run this dialog pops out but for data is just see bank. 当我运行此对话框时,弹出对话框,但有关数据,请参阅bank。 Noe data. Noe数据。 Can you please let me how to pass array of objects in the dialog. 您能让我如何在对话框中传递对象数组吗? My solution is based on other solutions here but no luck. 我的解决方案基于此处的其他解决方案,但没有运气。

Note: Everything works fine there is no error except no array of object being passed. 注意:一切正常,除了没有传递对象数组外,没有任何错误。

You are not anywhere setting values for this.prod , so it will not have a value. 您在任何地方都没有为this.prod设置值,因此它将没有值。 So you either need to pass prod in the openDialog , or then assign the parameter from the method to this.prod : 因此,您需要在openDialog传递prod ,或者将方法中的参数分配给this.prod

openDialog(prod:any): void {
  this.prod = prod; // here!!
  let dialogRef = this.dialog.open(prodDialog, {
    width: '400px',
    height: '500px;',
    data: this.prod  // now 'this.prod' will have values!
  }); 
}

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

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