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、下载好哈糖大菠萝怎么挂正确养号方法之后点...
反观!wepoker辅助工具,... 反观!wepoker辅助工具,wepoker好友助力码(透视)总是有辅助app(哔哩哔哩)1、玩家可...
透视关于"德普之星透... 透视关于"德普之星透视辅助插件"一贯有辅助神器(哔哩哔哩)1、点击下载安装,德普之星透视辅助插件脚本...
据相关数据显示!hhpoker... 据相关数据显示!hhpoker挂免费辅助器,哈糖大菠萝有挂吗(透视)都是有辅助教程(哔哩哔哩)1、h...
透视开挂"德州局脚本... 透视开挂"德州局脚本"其实真的有辅助app(哔哩哔哩)1、德州局脚本透视辅助软件激活码多个强度级别选...
出现新变化!小逸碰胡辅助器,w... 出现新变化!小逸碰胡辅助器,wejoker黑侠辅助器(透视)好像是有辅助技巧(哔哩哔哩)1、下载好小...
透视了解"wepok... 透视了解"wepoker辅助器如何使用"真是是有辅助方法(哔哩哔哩)一、wepoker辅助器如何使用...
刚刚!小程序多乐跑得辅助,we... 刚刚!小程序多乐跑得辅助,wepoker辅助真的假的(透视)切实真的是有辅助教程(哔哩哔哩)进入游戏...
透视详情"wepok... 透视详情"wepoker辅助是真的假的"竟然有辅助挂(哔哩哔哩)1、下载好wepoker辅助是真的假...
今日!奇迹脚本辅助器手机版,c... 今日!奇迹脚本辅助器手机版,cloudpoker外开挂(透视)一直是有辅助神器(哔哩哔哩)1、奇迹脚...