I'm a beginner for angular 11, And I want to get data from Mat-Dialog .open()
then convert as object of Goods class array to another component (ProductListComponent)
My interface class:
export interface Goods {
goodsId?: number;
goodsCode?: string;
goodsName?: string;
goodsGroupId?: number | null;
goodsCateName?: string;
goodsGroupName?: string;
}
I assign Array Object to dialogConfig.data
(at first goodsSelect variable was empty and will assigned when afterClosed()
has called.)
export class PromotionAddComponent implements OnInit {
goodsSelect?: Goods[];
openDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.data = this.goodsSelect;
const dialogRef = this.dialog.open(ProductListComponent, dialogConfig);
dialogRef.afterClosed().subscribe(data => {
this.goodsSelect = data;
});}}
Receive data from MAT_DIALOG_DATA
in this component
but there can't use .filter()
.forEach()
or any Array function.
I tried Object.value()
but not working, there has no error and no console log display.
And tried @Inject(MAT_DIALOG_DATA) private data: Goods[]
also not working.
export class ProductListComponent implements OnInit {
public allGoods?: Goods[];
public selectedItem?: Goods[];
displayedColumns: string[] = ['select', 'goodCateName', 'goodGroupName', 'goodCode', 'goodName', 'goodUnitName'];
dataSource = new MatTableDataSource<Goods>([]);
selection = new SelectionModel<Goods>(true, []);
constructor(
private goodsService: GoodsService,
private dialogRef: MatDialogRef<ProductListComponent>,
@Inject(MAT_DIALOG_DATA) private data: any) { }
ngOnInit(): void {
this.selectedItem = this.data as Goods[];
for(let item of this.selectedItem){
console.log(item.goodCode + " => " + item.goodName1);
}
this.dataSource.data.forEach(item => {
let res = this.selectedItem!.filter((e) => e.goodId == item.goodId);
if (res.length > 0) {
this.selection.select(item);
console.log("this.selectedItem was matched.");
}
});
}
}
This error show when trying to read each element include .map()
.forEach()
.for()
https://i.stack.imgur.com/EPlZU.png
console.log()
this.data variable, there had data but I can't read or convert this object.
Where did I got mistake ?