igniteui-angular icon indicating copy to clipboard operation
igniteui-angular copied to clipboard

ExpressionChangedAfterItHasBeenCheckedError shown in console when using the filtering and searching API of the grid

Open lalo-mx opened this issue 8 years ago • 3 comments

Description

When implementing the filtering and searching API of the grid an error happens if you type and delete search terms fast.

  • igniteui-angular version: 6.1.3
  • browser: Chrome 68.0.3440.106

Steps to reproduce

  1. Open demo in StackBlitz
  2. Type a search in the input of at least ten characters and then delete all characters fast.

Result

An error is shown in the console:

10:57:15.410 preview-fe7237b13d780dbf847da.js:1
 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: false'. Current value: 'ngIf: true'.
    at viewDebugError (errors.ts:35)
    at expressionChangedAfterItHasBeenCheckedError (errors.ts:21)
    at checkBindingNoChanges (util.ts:102)
    at checkNoChangesNodeInline (view.ts:473)
    at checkNoChangesNode (view.ts:461)
    at debugCheckNoChangesNode (services.ts:464)
    at debugCheckDirectivesFn (services.ts:392)
    at Object.eval [as updateDirectives] (GridSearchSampleComponent.html:13)
    at Object.debugUpdateDirectives [as updateDirectives] (services.ts:384)
    at checkNoChangesView (view.ts:341)

Expected result

The error should not be thrown.

Attachments

Attach a sample if available, and screenshots, if applicable.
image

lalo-mx avatar Aug 10 '18 16:08 lalo-mx

@lalo-mx This is a bug in the sample. Basically, it is an issue that happens when a value that affects the DOM changes during the change detection cycle, but after the value has been checked. It can be resolved by simply injecting ChangeDetectorRef into your component and running a new change detection cycle. I have attached a modified version of the StackBlitz sample that demonstrates how to do that.

Also, if you want more information on this error, you can look up this article where the error is explained in depth.

igx-grid-with-searching.zip

DDavidkov avatar Aug 14 '18 12:08 DDavidkov

@DDavidkov Thanks, it is working now with your modifications. I created the issue because search and filtering works perfectly if they are run separately but the fix definitely works.

lalo-mx avatar Aug 14 '18 13:08 lalo-mx

@lalo-mx Yeah, it is a very strange issue. My best explanation as to why this happens only when both features are active is that the filtering is actually creating new IgxGridRowComponents while you are deleting the filter and this causes additional change detection cycles, in which changes in the length of the lastSearchInfo.matchInfoCache array of the grid are somehow interpreted to be happening after the value has been checked.

Anyway, since my fix seems to be working for you, I am going to close this issue.

DDavidkov avatar Aug 14 '18 16:08 DDavidkov