【问题标题】:How to discard or clear MatDailog data after close or after submit关闭或提交后如何丢弃或清除 MatDailog 数据
【发布时间】:2019-03-25 14:20:55
【问题描述】:

我有一个叫customers(dialog window)的组件,里面有两个叫listdisplay的组件,我会从list组件将一些对象推送到 display 组件中的 table,如下所示:

组件代码:

list.component.html

<h3>List</h3>
<form [formGroup]="addListForm">
<mat-form-field>
  <mat-select  formControlName="CustomerIds" placeholder="Select Customer" multiple>
    <mat-option *ngFor="let customer of customers" [value]="customer" >
      {{customer.name}}
    </mat-option>
  </mat-select>
</mat-form-field>
<br>
<button  (click)="onAdd()">Add</button>
</form>

list.component.ts

import {Component,  OnInit } from '@angular/core';
import { FormBuilder,FormGroup } from '@angular/forms';
import { ContactService } from '../contacts.service';
import { MatOptionSelectionChange } from '@angular/material';
import { ICustomer } from '../models';
import { DataService } from '../data.service';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.css']
})
export class ListComponent implements OnInit {
public customers: ICustomer;
public someCustomer: ICustomer;
public addListForm: FormGroup;

constructor(private fb: FormBuilder,
  private myService: ContactService,
   public dataService: DataService) { }

  public async ngOnInit(): Promise<void> {
    this.addListForm = this.fb.group({
      CustomerIds: [null],
    });
    this.customers = await this.myService.getCustomers('');
  }
  public async selected(event: MatOptionSelectionChange, customer: ICustomer): Promise<void> {
    this.myService.onCustomerSelect.next(customer);
  }

    public onAdd(): void {
    this.someCustomer = this.addListForm.value;
    this.dataService.onSelectCustomer.next(this.someCustomer);
  }

}

display.component.html

<table mat-table [dataSource]="selectedCustomers?.CustomerIds" >
            .....
  </table>

display.component.ts

import {Component,  OnInit } from '@angular/core';
import { FormBuilder,FormGroup } from '@angular/forms';
import { ICustomer } from '../models';
import { DataService } from '../data.service';
@Component({
  selector: 'app-display',
  templateUrl: './display.component.html',
  styleUrls: ['./display.component.css']
})
export class DisplayComponent implements OnInit {
public  contacts: ICustomer;
public selectedCustomers: any;
public displayForm: FormGroup;
public addCustomer: any;

public displayedColumns: string[] = ['name', 'email', 'phone', 'button'];

constructor(private fb: FormBuilder,public dataService: DataService) { }

  public async ngOnInit(): Promise<void> {
    this.displayForm = this.fb.group({
    });
   this.dataService.onSelectCustomer.subscribe(value => {
   this.selectedCustomers = value;
  });
  }

}

现在,我将把一些对象(客户)从 list 组件推送到 display 组件,然后我将关闭对话框窗口。如果我再次打开对话框窗口,表格数据必须在没有任何先前从 list 组件推送的数据的情况下清除。如何清除表缓存?

Stackblitz DEMO

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    在您的主文件 app.component.ts 中使用 MatDialog 的 afterClosed() 方法,每次关闭对话框时都会调用该方法以清空表的旧数据。

    public openAdd(): void {
      const dialogRef: MatDialogRef<CustomersComponent> = this.dialog.open(CustomersComponent, {
        width: '80vw', height: '80vh',
      });
      dialogRef.afterClosed().subscribe(result => {
        this.dataService.onSelectCustomer.next([]); 
      });
    }
    

    【讨论】:

      【解决方案2】:

      没有你想象的缓存问题。您正在使用服务在组件之间共享数据,因此现有的推送值到 next() 中仍然可以使用这些值或数据。你可以阅读更多关于Services的信息。

      所以如果要清除现有数据那么有两种方法:

      解决方案 1:

      如 FarhatZaman 所述,您可以订阅 dialogRef.afterClosed() 方法:

      dialogRef.afterClosed().subscribe(res => {
            this.dataService.onSelectCustomer.next([]);
       })
      

      解决方案 2:

      您可以像这样清除方法中的数据:

      public onSave(): void {
         this.addCustomer = this.selectedCustomers;
         this.dataService.onSelectCustomer.next([]); // Here
      }
      

      Stackblitz

      【讨论】:

      • 对不起,我给了旧的工作演示,在新的工作演示中==> stackblitz.com/edit/…,关闭对话框窗口后清除table数据工作正常,但又一次我无法将其添加到table
      • 请考虑这个链接 ===> stackblitz.com/edit/… @Prashanth Pimpale
      • @Prashanth 已在此处修复错误:stackblitz.com/edit/…
      • 我试过else condition,但我忘了检查value.CustomerIds,无论如何我得到了预期的结果,谢谢你的回复。
      猜你喜欢
      • 2018-05-16
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2020-09-09
      • 2019-12-03
      • 1970-01-01
      相关资源
      最近更新 更多