无法使用角度材料读取角度为2的未定义属性'sdeptname'

时间:2018-03-30 11:44:46

标签: angular angular-material

我使用角度材料在角度2中实现了简单的表格...我已经采用了两个mat-table,其中第一个表格中的选定行在点击移动到表格2 时转移到第二个表格反之亦然点击移至表1

但是当我选择并单击移动到表2 时,我的第一个表中的所有行都会被拼接,我在控制台中收到以下错误,如下所示。

https://codepen.io/AlexWulkan/pen/wmmPvL

请访问我的示例enter image description here

1 个答案:

答案 0 :(得分:1)

尝试使用HTML中的嵌套对象,例如element?.sdeptname。 这确保即使元素的值为null,有时它也不会在您尝试访问NULLundefined对象时抛出错误。

使用elvis运算符?.至少可以消除html

中出现的错误

<强>更新

修正了你的一个功能:

  moveToTableTwo() {

      console.log(this.dataSource.data)
      this.selection.selected.forEach((k,item) => {

        this.dataSource.data.splice(this.dataSource.data.indexOf(k),1); 
        this.checkedDataSource.data.push(k);
      });
      console.log(this.dataSource.data)
      this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
      this.checkedDataSource = new MatTableDataSource<Element>(this.checkedDataSource.data);

  }

尝试使用其他功能。

<强>更新

你的其他功能应该是这样的:

moveToTableOne() {

  this.checkedSelection.selected.forEach((k,item) => {this.checkedDataSource.data.splice(this.checkedDataSource.data.indexOf(k),1);
        this.dataSource.data.push(k);
      });
      this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
      this.checkedDataSource = new MatTableDataSource<Element>(this.checkedDataSource.data);


} }