我有一个名为customers(dialog window)
的组件,其中有两个组件分别为 list 和 display ,来自 list 组件我将像这样将某些对象推入显示组件中的表:
组件代码:
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 组件中推送的数据。如何清除表缓存?
答案 0 :(得分:1)
在主文件app.component.ts
中,使用MatDialog的afterClosed()
方法,每次关闭Dialog时,该方法都会被调用以清空表的旧数据。
public openAdd(): void {
const dialogRef: MatDialogRef<CustomersComponent> = this.dialog.open(CustomersComponent, {
width: '80vw', height: '80vh',
});
dialogRef.afterClosed().subscribe(result => {
this.dataService.onSelectCustomer.next([]);
});
}
答案 1 :(得分:1)
您认为与缓存无关。您正在使用Service在组件之间共享数据,因此将现有的推入值放入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
}