我正在使用我要检查的Angular 7应用程序,我需要检查
如果在component.html上使用* ngIf,则在Report Control对象上是否存在Reportid。
在report.component.ts
上:
displayreport: any = {};
Reportid: string;
ngOnInit() {
this._displayreport.GetReportControl(Reportid).subscribe((res: any) => {
this.ReportControl = res;
console.log("report control is" + JSON.stringify(this.ReportControl)
});
}
}
this.ReportControl
仅返回一个对象为:
{"reportid":"2040","reportName":"financialAsset","reportType":"1"}
预期结果:
*ngIf="???????"
displayreport: any = {};
Reportid: string;
ReportControl: any;
ngOnInit() {
this._displayreport.GetReportControl(this.Reportid).subscribe((res: any) => {
this.ReportControl = res;
console.log("report control is" + JSON.stringify(this.ReportControl)
});
}
}
如果您正在检查html(ngIf)的响应中是否有reportid
值,则可以通过以下操作来实现:
*ngIf="ReportControl && ReportControl.reportid"
*ngIf="ReportControl?.reportid"
[两者都以类似的方式工作,他们先检查ReportControl
是否存在,然后再检查相应的reportid
是否存在,这种方法更可靠,因为它避免了未定义的属性错误。
用作* ngIf =“ Report controller?.reportid”