我想我知道你有点问题。如果您使用 @Input 或将由 rest-call 设置的属性:
@Input() data: Array<YourInterface>;
或
public data: Array<YourInterface>;
...
public onInit() {
httpClient.get(configuration).subscribe(data => {
this.data = data;
}
}
那么直接在你的模板中使用这个数据属性是没有用的,因为在你通过你的应用程序的 ui 部分修改它之前,你无法确定这个数据属性的状态。
不要直接使用它,而是这样做:
public inputData: Array<YourInterface>;
@Input() data: Array<YourInterface>;
...
public onInit() {
this.inputData = {...this.data};
}
或
public inputData: Array<YourInterface>;
public data: Array<YourInterface>;
...
public onInit(): void {
httpClient.get(configuration).subscribe(data => {
this.data = data;
this.inputData = {...this.data};
}
}
并在您的模板中使用inputData,而不是使用data。
然后添加一个重置方法,您可以使用该方法将数据重置为使用 ui 操作之前的状态(将此方法连接到重置按钮将重置所有行)。
resetData(): void {
this.inputData = {...this.data};
}
然后使用一种方法来持久化您的数据。
saveData(): void {
this.data = {...this.inputData};
...
// more steps to persistence
// make a http.post or emit this.data
}
编辑:我假设你得到一个包含任何东西的数组,这个数组的每个条目都是一个对象并且有一个模型,以将其显示为表格。
界面:
interface YourInterface {
id: number;
name: string;
tel: string;
}
样本数据:
let data: Array<YourInterface> = [
{
id: 0,
name: 'A name',
tel: '+892383498239'
},
{
id: 1,
name: 'Another name',
tel: '+23298238923'
}
];