Angular objec在Chrome中为空,在IE中不为空
我有一个非常奇怪的问题,我无法理解,当使用“Chrome”时,我的对象有时会变为空,但在IE中它总是工作的。这只会发生在我的所有下拉列表中,然而,其他控件如textbox正在按预期工作…这是我的代码的简化版本 注意:dropcrown绑定正确,因此这些值在html中可用 HTML(使用反应式表单) JSONAngular objec在Chrome中为空,在IE中不为空,angular,typescript,primeng,primeng-dropdowns,Angular,Typescript,Primeng,Primeng Dropdowns,我有一个非常奇怪的问题,我无法理解,当使用“Chrome”时,我的对象有时会变为空,但在IE中它总是工作的。这只会发生在我的所有下拉列表中,然而,其他控件如textbox正在按预期工作…这是我的代码的简化版本 注意:dropcrown绑定正确,因此这些值在html中可用 HTML(使用反应式表单) JSON { "result": [ { "cost_center_id": 1, "cost_center_name": "1" }, {
{
"result": [
{
"cost_center_id": 1,
"cost_center_name": "1"
},
{
"cost_center_id":2,
"cost_center_name": "2"
}
]
}
我认为这是因为
iCostCenter
在您的订阅中填充,并且populateheaderdt()
在getAllCostCenters()返回响应之前被调用
在分配iCostCenter后,尝试调用populateHeaderSet()
ngOnInit() {
this.getAllCostCenetrs();
}
getAllCostCenetrs() {
this._appParams.getAllCostCenters().subscribe(
data => {
this.iCostCenter = data.result;
this.populateHeaderDet();
},
error => 'GetAllCostCenetrs Method: ');
}
populateHeaderDet()
{
this.ersaForm.patchValue({
costCenter: this.iCostCenter.find(cc => cc.cost_center_name === this.iRequest.costCenter.cost_center_name)
)};
编辑
从注释中可以看出,您实际上想要做的是等待多个可观察对象完成,然后再调用populateheaderdt()
方法。解决这一问题的一种方法是使用该方法
由于我还没有看到您的代码的其余部分,因此很难确定,但您可以尝试这样的方法
const costCentres$ = this._appParams.getAllCostCenters();
const companyCodes$ = this._appParams.getAllCompanyCodes()
zip(
costCentres$,
companyCodes$,
(costCentres: string[], companyCodes: string[]) => ({costCentres, companyCodes}))
.subscribe(data => {
this.iCostCenter = data.costCentres;
this.companyCodes = data.companyCodes;
this.populateHeaderDet()
});
尝试调用this.getAllCostCenetrs();此.populateHeaderSet()
在ngoonchanges
中,而不是ngOnInit
中,这也不起作用,因为问题是随机的,与时间有关。换句话说,这个.getAllCostCenetrs()有时不会完成装载。那么,我该如何强制它检查.getAllCostCenetrs()是否已完成加载,然后调用PopulateHeaderSet()对不起,我之前没有说过我想要的…您的解决方案会起作用,但我如何使用abrovable来检查它是否已完成加载not@rgoal对不起,我不知道你在问什么?对不起,有没有别的办法。在我的真实代码中,我有6个dropwdown,然后是这个;被调用,这使得它在订阅中有点混乱。所以在我的情况下,我需要从subscribe一次调用一个dropdpwn…这有意义吗!所以基本上我需要从DropDown a的subscribe中调用DopDown b。然后我必须从DropDown b的subscribe方法中调用drowdown c。所以基本上我只是想找出是否有更好的方法来实现这一点
ngOnInit() {
this.getAllCostCenetrs();
}
getAllCostCenetrs() {
this._appParams.getAllCostCenters().subscribe(
data => {
this.iCostCenter = data.result;
this.populateHeaderDet();
},
error => 'GetAllCostCenetrs Method: ');
}
populateHeaderDet()
{
this.ersaForm.patchValue({
costCenter: this.iCostCenter.find(cc => cc.cost_center_name === this.iRequest.costCenter.cost_center_name)
)};
const costCentres$ = this._appParams.getAllCostCenters();
const companyCodes$ = this._appParams.getAllCompanyCodes()
zip(
costCentres$,
companyCodes$,
(costCentres: string[], companyCodes: string[]) => ({costCentres, companyCodes}))
.subscribe(data => {
this.iCostCenter = data.costCentres;
this.companyCodes = data.companyCodes;
this.populateHeaderDet()
});