本文介绍了可观察到的Angular服务返回嵌套数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的端点包含这样
{
'dsco_st_license': {
'ttco_st_license': [
{
'csl_state': 'AK',
'csl_license_name': 'Specialty Contractor',
'csl_license_code': 'Communications',
'csl_license_number': '123456'
},
{
'csl_state': 'AL',
'csl_license_name': '',
'csl_license_code': '',
'csl_license_number': 'Not Required'
}
]
}
}
这是我的服务
ngOnInit() {
this._WebLicenseServiceService.getWebLicensesHttpCall()
.subscribe(data => this.weblicenses = data,
err => console.error('Observer got an error: ' + err),
() => console.log('close loading spinner')
);
console.log(this.weblicenses);
}
当我console.log(this.weblicenses)时;它不包含数据.相反,看起来像这样
when i console.log(this.weblicenses);it dosnt contain the data. Instead looks like this
[]length: 0__proto__: Array(0)
我也尝试过
console.log(this.weblicenses ['dsco_st_license'] ['dsco_st_license']);
console.log(this.weblicenses['dsco_st_license']['dsco_st_license']);
但这是由于我的界面而导致的错误
But this errors out because of my interface
export interface IWeblicense {
csl_state: string;
csl_license_name: string;
csl_license_code: string;
csl_license_number: string;
}
如何获得ttco_st_license退回?
How can I get the ttco_st_license to be returned?
推荐答案
访问订阅函数中的响应数据,以确保响应完成后将执行该响应数据.
Access the response data inside subscribe function which ensure it will be executed once the response is completed.
ngOnInit() {
this._WebLicenseServiceService.getWebLicensesHttpCall()
.subscribe(data => {
this.weblicenses = data;
console.log(this.weblicenses); //<--- Get the data after call completes.
console.log(this.weblicenses.dsco_st_license. ttco_st_license); //<---ttco_st_license
},
err => console.error('Observer got an error: ' + err),
() => console.log('close loading spinner')
);
// console.log(this.weblicenses); //<-- Remove this since it will return undefined
}
这篇关于可观察到的Angular服务返回嵌套数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!