我的端点包含这样
{
'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); 它不包含数据。相反,看起来像这样
[]length: 0__proto__: Array(0)
我也尝试过
console.log(this.weblicenses ['dsco_st_license'] ['dsco_st_license']);
但这是由于我的界面而导致的错误
export interface IWeblicense {
csl_state: string;
csl_license_name: string;
csl_license_code: string;
csl_license_number: string;
}
如何获得ttco_st_license退回?
答案 0 :(得分:1)
访问订阅函数内部的响应数据,以确保响应完成后将执行该数据。
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
}