您好,我正在尝试将对象数组传递到 Material 对话框。这是我的做法。
Model
export interface IPoducts{
recordname: string;
comments: [{
comment: string
}]
}
在组件类中有以下相关内容
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
});
}}
对话框组件。
@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>
对话框是
<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>
<button md-raised-button (click)="onCloseCancel()">CANCEL</button>
</md-dialog-actions>
</div>
当我运行时,会弹出此对话框,但对于数据,只需查看银行。诺伊数据。您能告诉我如何在对话框中传递对象数组吗?我的解决方案基于这里的其他解决方案,但没有运气。
注意:一切正常,除了没有传递对象数组之外没有错误。
最佳答案
您没有在任何地方为 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!
});
}
关于javascript - Angular 4 Material 对话框将对象数组传递到对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47824920/