加载组件后,表达式发生了变化

时间:2016-10-07 13:39:18

标签: angular typescript

我喜欢问类似问题,但无法解决我的问题。

@Component({
  selector: 'waiting',
  template: `
  <div class="waiting">
    <div *ngIf="isLoading" class="loader2"></div>
  <div>`
})

export class WaitingComponent {
  public isLoading: boolean;

  public constructor(private _authHttp: HttpService) {
    this._authHttp.request.subscribe((action: HttpAction) => {
      this.isLoading = action === HttpAction.Requested;//HttpAction.Requested returns 0 or 1
    });
  }
}

我已经看到过必须在AfterViewInit中更改值isLoading的建议。我无法弄清楚如何让它发挥作用。提前致谢。

1 个答案:

答案 0 :(得分:0)

要解决此问题,您可以使用ChangeDetectorRef.detectChanges()zone.run(...)

export class WaitingComponent {
  public isLoading: boolean;

  public constructor(private _authHttp: HttpService, cdRef:ChangeDetectorRef) {
    this._authHttp.request.subscribe((action: HttpAction) => {
      this.isLoading = action === HttpAction.Requested;//HttpAction.Requested returns 0 or 1
      cdRef.detectChanges();
    });
  }
}

但我认为真正的问题出在_authHttp