【发布时间】:2019-12-06 00:01:58
【问题描述】:
我希望通过参数 id 更改我的 url。问题是数据很大,所以我想在开始时加载整个数据。但是我发现一旦 url 更改,代码就会转到 ngOnInit 并且数据丢失。所以我必须重新加载它,它非常慢。所以我创建了一个全局服务类来存储数据,如果 url 发生变化,那么我比较数据是 null 还是 undefined。如果它为空,那么我从全局服务中检索数据。当然,我一开始就存储数据。它看起来不错。但是我发现了一件很疯狂的事情,如果代码在 getDataFromGlobalService 方法上运行,有时表单或文本框上的绑定会丢失。我想可能是异步问题或其他问题。
const url = 'apps/mycase/';
this.router.navigate([url], {queryParms: {id: this.id}});
if(this.data)
this.loadingData();
else
this.getDataFromGlobalService();
ngOnInit() {
this.data = this.globalService.getData();
this.loadDropDownData();
}
getDataFromGlobalService 方法
private getDataFromGlobalService() {
// here still binding
if(!this.data) {
this.service['some'].get().subscribe(d => {
this.data = d;
this.globalService.setBackupData(this.data);
};
}
else { // the binding lost here if code arrive here
otherthings();
}
}
The global service is simple.
@Injectable()
export class GlobalService {
public data: any;
constructor() {
this.data = null;
}
public setBackData = (data: any) => {
this.data = data;
}
public getData = () => {
return this.data;
}
}
}
【问题讨论】:
标签: angular data-binding