如何检查ReportControl对象上是否存在Reportid,或者不使用* ngif?

问题描述 投票:1回答:2

我正在使用我要检查的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="???????"
javascript typescript angular7 angular-directive angular-components
2个回答
2
投票
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值,则可以通过以下操作来实现:

  1. *ngIf="ReportControl && ReportControl.reportid"
  2. *ngIf="ReportControl?.reportid"

[两者都以类似的方式工作,他们先检查ReportControl是否存在,然后再检查相应的reportid是否存在,这种方法更可靠,因为它避免了未定义的属性错误。


0
投票

用作* ngIf =“ Report controller?.reportid”

© www.soinside.com 2019 - 2024. All rights reserved.