Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpressionChangedAfterItHasBeenCheckedError after changing the formControl (Reactive Form) #139

Closed
chrisschauer opened this issue Oct 1, 2019 · 11 comments · Fixed by #140
Assignees
Labels
Milestone

Comments

@chrisschauer
Copy link

Hi,

i use ckeditor component in a reactive form, where i can also switch the edit language of the form.
A field e.g. a "Title" is a form group with multiple controls named like "de, en, hu, ...":

<div *ngIf="editLanguage | async as lang">
    <ckeditor [formGroup]="control" 
              [editor]="EditorLongDescriptionInline"
              [formControl]="control.get(lang.name)">
    </ckeditor>
</div>

The first language change works quite well. After the first one changes of the edit language leads to this error on console:

13:32:27,385 ERROR admin platform.GlobalErrorHandler: handleError: "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-pristine: true'. Current value: 'ng-pristine: false'."

Can't see this issue on the material input fields. Anything i can check?
Thx

Angular: 7.2.9
CKEditor-Angularr: 1.1.0
CKEditor-inline: 12.3.0

@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 3, 2019

Hi @chrisschauer.

It's the first time I see this error, I'll check it.

@ma2ciek ma2ciek self-assigned this Oct 3, 2019
@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 3, 2019

I see that dynamically toggling the value of the <ckeditor> component doesn't work well with Reactive Forms. I don't know why as Reactive Forms rely on ControlValueAcecessor, same as e.g. the ngForm, which works well in that scenario. I'll try to investigate it more, maybe there's a bug in the current implementation of this interface or maybe the async editor nature somehow conflicts with the sync nature of Reactive Forms.

@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 3, 2019

TBH, I didn't manage to produce the ExpressionChangedAfterItHasBeenCheckedError error, but the above finding seems to be connected.

@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 3, 2019

Ok, I understand at least my bug. Selecting new language triggers setting new data on the component, which causes changing editor data which fires change:data just after the change. The <ckeditor> component listens to this event and reacts to it calling among others the cvaOnChange() (ControlValueAccessor onChange callback). And this mechanism, unfortunately, overwrites the data of the just selected editor. A simple lock mechanism fixes it. I'll create a PR. Hopefully, it will fix your error as well.

@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 4, 2019

@chrisschauer, could you test if the above PR fixes your issue?

@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 4, 2019

Actually the PR makes the two-way binding with [(ngModel)] more accurate and less bug-prone.

@mlewand mlewand added this to the backlog milestone Oct 7, 2019
@chrisschauer
Copy link
Author

Thank you very much for effort of investigation @ma2ciek.
Sorry for the delayed answer - i try to test the PR today.

Thx

@chrisschauer
Copy link
Author

Hi @ma2ciek

indeed your PR #140 solves the problem while changing the language and also the exception disappeared. I really appriciate your work. Thanks for the quick fix.

Will be there a new release including the fix in the near future?

pomek added a commit that referenced this issue Oct 15, 2019
Fix: The `<ckeditor>` component won't call the CVA `registerOnChange()` when the change comes from the CVA. This will fix an issue with changing data in Reactive Forms. Closes #139.
@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 15, 2019

Thanks, @chrisschauer,

I need to take a look at #141 and its PR and once it is resolved I'll release a new version, so it should be available within 1-2 weeks.

@ma2ciek ma2ciek modified the milestones: backlog, iteration 27 Oct 31, 2019
@ma2ciek
Copy link
Contributor

ma2ciek commented Oct 31, 2019

Update: The latest release includes the fix - "@ckeditor/ckeditor5-angular": "1.1.2"

@chrisschauer
Copy link
Author

Works perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants