0

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 ?

https://i.stack.imgur.com/JxpdZ.png

  • Apparently there's an extra `data` property in your object. Try `this.selectedItem = this.data.data as Goods[];` – Octavian Mărculescu Oct 05 '21 at 08:03
  • @OctavianMărculescu You’re so helpful!, and I have some question. When use those function `.forEach()` `.filter()`, `for()`, Should I wrap variable with `Object.values()` for every time or there're have another way ? Because if unwrap it not work. Thank you a lot. – Kantrakorn J Oct 05 '21 at 09:25

0 Answers0