【问题标题】:Expression has changed after it was checked Loading componennt选中加载组件后,表达式已更改
【发布时间】:2016-10-07 21:51:04
【问题描述】:

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

@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。我不知道如何使它工作。提前致谢。

【问题讨论】:

  • 此代码不应导致该错误。必须有别的东西。
  • this.isLoading = action === HttpAction.Requested; 感觉这条线有问题。
  • 这个this.isLoading = HttpAction.Requested怎么样?
  • @micronyks this.isLoading = HttpAction.Requested 将返回和异常,因为 HttpAction.Requested 的返回值是数字,0 或 1。this.isLoading = action === HttpAction.Requested; 工作正常,没有问题。
  • @GünterZöchbauer 不幸得到inline template:2:9 caused by: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'.

标签: angular typescript


【解决方案1】:

要解决您的问题,您可以使用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

【讨论】:

  • 这成功了。也许你是对的,会看看。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 2017-08-28
  • 2018-08-02
  • 2017-09-16
  • 1970-01-01
  • 2020-07-13
相关资源
最近更新 更多