简体   繁体   English

如何在不重新加载 Angular 中的页面的情况下刷新 UI

[英]how to refresh UI without reloading the page in Angular

I have a multiple charts in my page and I'm trying to make a delete call but some reason my chart UI is not updating immediately when I click the delete button.我的页面中有多个图表,我正在尝试进行删除调用,但由于某种原因,当我单击删除按钮时,我的图表 UI 没有立即更新。 I always need to refresh the browser in order to see the changes.我总是需要刷新浏览器才能看到更改。

I uploaded the full code for this two component her https://stackblitz.com/edit/angular-ivy-nnun96 so I would be really appreciated if I can get any suggestion on how to make the UI remove the Chart immediately when the user press Delete button.我上传了这两个组件的完整代码她的https://stackblitz.com/edit/angular-ivy-nnun96所以如果我能得到任何关于如何让用户界面立即删除图表的建议,我将不胜感激按删除按钮。

Mc Chart List TS Mc Chart 列表 TS

  deleteChart(){
    this.chartService.deleteChart(this.chart.guid).subscribe((deleted) => {
      console.log(deleted);
    });
  }

Mc Chart List HTML麦克图表列表 HTML

 <button mat-menu-item (click) = "deleteChart()" *ngIf = "chart.hasAccess && chart.canEdit && !chart.isPublished">Delete Chart</button>

Parent HTML父 HTML

       <mc-chart-list [chart]="chart" [editMode]="true" [wsType]="workspace.type"></mc-chart-list>

Parent TS家长 TS

ngOnInit(): void {
this.charts = this.workspace.charts;
}

It look like this right now现在看起来像这样

在此处输入图像描述

You can use ChangeDetectorRef to detect changes on the view.您可以使用 ChangeDetectorRef 来检测视图上的更改。

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

constructor(private ref: ChangeDetectorRef)

deleteChart(){
    this.chartService.deleteChart(this.chart.guid).subscribe((deleted) => {
      console.log(deleted);
      this.ref.detectChanges();
    });
  }

Note: Remove changeDetection: ChangeDetectionStrategy.OnPush (if you are using it)注意:删除changeDetection:ChangeDetectionStrategy.OnPush(如果你正在使用它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM