简体   繁体   English

角度中的 getter 值抛出 ExpressionChangedAfterItHasBeenCheckedError

[英]getter value in angular throws ExpressionChangedAfterItHasBeenCheckedError

I'm getting ExpressionChangedAfterItHasBeenCheckedError for the getter value I'm using in my component我在组件中使用的 getter 值收到 ExpressionChangedAfterItHasBeenCheckedError

public get selectedRows() {
  if (this.gridApi) {
    return this.gridApi.getSelectedRows();
  } else {
    return null;
  }
}

Here is my HTML code where I'm using this selectedRows这是我使用这个 selectedRows 的 HTML 代码

<div *ngIf="selectedRows && selectedRows.length > 0" class="btn-cls">
  <button>submit</button>
</div>

I tried [hidden] instead of *ngIf but still getting the same error.我尝试了 [hidden] 而不是 *ngIf 但仍然遇到相同的错误。

The exception thrown is how Angular prevent inconsistency between the data model and view, therefore wrong or old data is not shown to the final user.抛出的异常是 Angular 如何防止数据模型和视图之间的不一致,因此错误或旧数据不会显示给最终用户。 Please check that link out: https://indepth.dev/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error/请检查该链接: https : //indepth.dev/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error/

The link above will help to understand how Angular avoid inconsistency in data during the change detection process, and will probably help you to solve your issue.上面的链接将有助于了解 Angular 如何在变更检测过程中避免数据不一致,并且可能会帮助您解决问题。

I was able to resolve my issue using ngDoCheck instead of getter我能够使用 ngDoCheck 而不是 getter 解决我的问题

ngDoCheck(): void {
  if (this.gridApi && this.gridApi.getSelectedRows() && this.gridApi.getSelectedRows().length > 0) {
    this.isRowSelected = true;
  } else {
    this.isRowSelected = false;
  }
}

HTML: HTML:

<div *ngIf="isRowSelected" class="btn-more">
  <button>submit</button>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM