中的角度未定义变量

时间:2019-07-26 11:21:23

标签: angular typescript

当我尝试用html调用dashboard.all_interviewed时,我从错误TypeError中收到错误:“ _ co.dashboard未定义”。

在“我的dashboard.component.ts”中

dashboard: Dashboard;

    getDashboard() {
      this.dashboardService.getDashboard().subscribe(
        res => {
            this.dashboard = res;
         }
      )
    }

在Dasboard课上

export class Dashboard {
   all_interviewed: number;
}

dashboard.component.html

<h2>{{ dashboard.all_interviewed }} </h2>

我在控制台日志中收到此错误。

  

错误TypeError:“ _ co.dashboard未定义”       View_DashboardComponent_0 DashboardComponent.html:208       角度32       RxJS 5       Angular 8 DashboardComponent.html:203:11

2 个答案:

答案 0 :(得分:0)

尝试以下代码,

<h2 *ngIf = "dashboard">{{ dashboard.all_interviewed }} </h2>

答案 1 :(得分:0)

getDashboard()上致电ngOnInit(),然后尝试以下操作:

<div *ngIf = "dashboard">
   <h2>{{ dashboard.all_interviewed }}</h2>
</div>