javascript - 如何检查 ReportControl 对象上是否存在 Reportid 或不使用 *ngif?
问题描述
我在 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)
});
}
}
如果您reportid
在 html(ngIf) 中的 response(res) 中检查该值是否可用,则可以通过以下方式实现:
*ngIf="ReportControl && ReportControl.reportid"
*ngIf="ReportControl?.reportid"
两者都以类似的方式工作,它们检查是否ReportControl
存在,然后检查对应的是否reportid
存在,这种方法更可靠,因为它避免了未定义的属性错误。
推荐阅读
- docker - Alpine docker image +755 无效模式下的 Linux 命令
- python - 如何使用正则表达式在 python 中正确替换
- scala - 如何不丢失异步调用?
- sql - 将用户定义的表作为选择语句传递给存储过程或函数
- laravel - 创建新项目时未填充本地文件夹
- html - 如何使 CSS 旋转动画 div 居中?
- kotlin - 无法访问类的父级伴随对象
- php - 为什么 1...2 等同于 PHP 中的“10.2”?
- node.js - 是否可以在前端更改时运行 nodemon 以重新启动服务器?
- jquery - Boostrap 4 Toast 调用后未显示