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

Change detection doesn't run when Model is updated. #241

Open
aakashyap opened this issue Nov 30, 2021 · 2 comments
Open

Change detection doesn't run when Model is updated. #241

aakashyap opened this issue Nov 30, 2021 · 2 comments

Comments

@aakashyap
Copy link

aakashyap commented Nov 30, 2021

I am trying to use ngx-monaco-editor to be able to load multiple files one at a time. I only have 1 instance of ngx-monaco-editor defined in my app-component.html file :

<ngx-monaco-editor [options]="options" [model]="model" [(ngModel)]="code"></ngx-monaco-editor>
<button (click)="load()">Load file </button>

On click of a button (one corresponding to each file) I update the model for Monaco-editor to display the new code:

In app-component.ts:

load(): void {
this.code = getCodeInClickedFile();
this.model = { ...this.model, value: this.code, language: 'json', uri: Uri.parse('a-b.json') };
}

As ngx-monaco-editor uses onPush strategy Issue-6, the above reference update of model object should have triggered Change detection. But the change detection is not triggered unless I press the buttons (or trigger any other event) twice.
This is not the case when I want to update options as mentioned in Issue-6 above. I believe this is due to the fact that when option is updated the reference of options is getting updated

this._options = Object.assign({}, this.config.defaultOptions, options);
whereas in case of model only the member property is updated which doesn't trigger onPush change detection strategy. The change takes effect only when a second event is triggered and change detection is executed.

Is my understanding correct? if not is there a workaround for code change to be visible as soon as the file is loaded (on click of a link or button)?

Thanks

@Disane87
Copy link

Disane87 commented Dec 1, 2021

We can reproduce this issue aswell. Actually I don't found any workaround.

@maks-humeniuk
Copy link

In my case, if I have both [(ngModel)]="value" and [model]="model" on <ngx-monaco-editor>, then changing value programmatically doesn't update the editor value on UI. To do that I have to update the whole model like this: this.model = { value: this.value };.

Please note that model change triggers editor blur event.

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

No branches or pull requests

3 participants