Angular mat-table在添加新行后不刷新
创始人
2024-10-19 13:00:43
0

要解决Angular mat-table在添加新行后不刷新的问题,您可以尝试以下方法:

  1. 使用Angular的ChangeDetectorRef:
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';

@Component({
  selector: 'app-your-component',
  templateUrl: './your-component.component.html',
  styleUrls: ['./your-component.component.css']
})
export class YourComponent implements OnInit {

  data: any[] = [];

  constructor(private cdr: ChangeDetectorRef) { }

  ngOnInit() {
  }

  addNewRow() {
    // 添加新行的逻辑
    // ...

    // 手动触发变更检测
    this.cdr.detectChanges();
  }
}
  1. 使用Angular的NgZone:
import { Component, OnInit, NgZone } from '@angular/core';

@Component({
  selector: 'app-your-component',
  templateUrl: './your-component.component.html',
  styleUrls: ['./your-component.component.css']
})
export class YourComponent implements OnInit {

  data: any[] = [];

  constructor(private ngZone: NgZone) { }

  ngOnInit() {
  }

  addNewRow() {
    // 添加新行的逻辑
    // ...

    // 在NgZone之外的上下文中运行变更检测
    this.ngZone.runOutsideAngular(() => {
      // 手动触发变更检测
      this.ngZone.run(() => {});
    });
  }
}

使用ChangeDetectorRef或NgZone中的任何一个方法都可以手动触发变更检测,从而更新mat-table的数据。这将确保添加新行后表格能够正确地刷新并显示新行。

相关内容

热门资讯

5分钟技法!wepoker轻量... 5分钟技法!wepoker轻量版透视系统(透视)一直真的是有辅助辅助(哔哩哔哩)1、进入游戏-大厅左...
第8分钟攻略!约局吧游戏挂(透... 第8分钟攻略!约局吧游戏挂(透视)切实存在有辅助教程(哔哩哔哩)1、玩家可以在约局吧游戏挂透视最简单...
三分钟指引!wepoker辅助... 三分钟指引!wepoker辅助分析器(透视)真是是真的有辅助工具(哔哩哔哩)1、玩家可以在wepok...
两分钟大纲!德州透视插件(透视... 两分钟大纲!德州透视插件(透视)果然真的是有辅助神器(哔哩哔哩)1、两分钟大纲!德州透视插件(透视)...
7分钟窍门!pokemmo手机... 7分钟窍门!pokemmo手机辅助软件(透视)确实真的有辅助开挂(哔哩哔哩)1、下载好pokemmo...
2分钟技法!wepoker有没... 2分钟技法!wepoker有没有挂(透视)本来真的是有辅助辅助(哔哩哔哩)1)wepoker有没有挂...
4分钟举措!wepoker辅助... 4分钟举措!wepoker辅助透视(透视)总是存在有辅助工具(哔哩哔哩)wepoker辅助透视辅助器...
第3分钟绝活儿!wepoker... 第3分钟绝活儿!wepoker安装教程(透视)好像是真的有辅助安装(哔哩哔哩)1、很好的工具软件,可...
两分钟指南书!wepoker游... 两分钟指南书!wepoker游戏下载(透视)好像是真的有辅助辅助(哔哩哔哩)1、操作简单,无需wep...
第8分钟诀窍!wpk辅助购买(... 第8分钟诀窍!wpk辅助购买(透视)本来真的是有辅助教程(哔哩哔哩)1)wpk辅助购买辅助插件:进一...