AngularMaterialTable和Paginator出现TypeError:无法读取未定义属性(读取'length')
创始人
2024-10-28 10:34:27
0

该问题的出现是在使用 Angular Material Table 和 Paginator 时,其中数据源没有正确地传递给数据表格。因此,在使用数据表格时会出现无法读取未定义属性(读取“length”)的错误。

解决这个问题的方法是:

1.确保在使用数据表格和分页器时,数据源被正确地传递给数据表格。

2.对于每个的数据源所绑定到的组件,需要对数据中的每个属性进行判空(null 或 undefined)的判断。

示例:

template.html

...

component.ts

import { Component } from '@angular/core';

import { MatTableDataSource } from '@angular/material/table';

import { MyModel } from './my-model';

@Component({

selector: 'app-table',

templateUrl: './table.component.html',

styleUrls: ['./table.component.css']

})

export class TableComponent {

dataSource = new MatTableDataSource([]);

ngOnInit() {

this.getData();

}

getData() {

// HTTP call to get data

this.myService.getData().subscribe((data) => {

//checking whether data is null or not

if (data) {

this.dataSource = new MatTableDataSource(data);

}

});

}

}

此示例中,我们正在从 HTTP 调用中获取数据并使用 MatTableDataSource 对其进行绑定。我们通过在一个判断中检查数据是否为空来解决 TypeError :无法读取未定义属性的错误。

相关内容

热门资讯

最新消息!拱趴大菠萝十三水辅助... 最新消息!拱趴大菠萝十三水辅助,永久免费脚本透视工具,好像真的有挂(解密分享教程)-哔哩哔哩1、完成...
透视有挂!hhpoker可以控... 透视有挂!hhpoker可以控制牌吗,德普之星app安卓版破解版,手筋教程(真是是有挂)-哔哩哔哩1...
透视最新版!约局吧德州有挂吗(... 透视最新版!约局吧德州有挂吗(透视)开挂辅助app(一贯有挂)-哔哩哔哩1、下载好约局吧德州有挂吗辅...
透视数据!pokerrrr2辅... 透视数据!pokerrrr2辅助,wepoker私人局怎么玩,学习教程(一贯是真的挂)-哔哩哔哩1、...
透视透视挂!wepoker怎么... 透视透视挂!wepoker怎么提高运气,欢乐达人暗堡链接辅助,一直真的有挂(专业力荐教程)-哔哩哔哩...
透视检测!aa poker辅助... 透视检测!aa poker辅助包(透视)开挂辅助技巧(原来真的是有挂)-哔哩哔哩1、aa poker...
透视工具!pokemmo脚本辅... 透视工具!pokemmo脚本辅助下载,德扑之心免费透视,学习教程(一直存在有挂)-哔哩哔哩1)pok...
透视ai代打!wepoker轻... 透视ai代打!wepoker轻量版有透视吗,牵手游戏辅助,一贯是有挂(了解必备教程)-哔哩哔哩暗藏猫...
透视规律!wepoker有用吗... 透视规律!wepoker有用吗(透视)开挂辅助器(切实真的是有挂)-哔哩哔哩透视规律!wepoker...
透视挂!pokerworld破... 透视挂!pokerworld破解版下载,哈糖大菠萝破解器,秘籍教程(确实真的有挂)-哔哩哔哩1、不需...