I am using the latest Angular along with the latest MonacoEditor in order to create an application that allows you to edit code directly in the browser. I had it all working great until today when I realized that the @ViewChild element that I am using to grab the DOM element and bind to the MonacoEditor was triggering hundreds of change detection cycles every second when hover/clicking/etc inside of the editor.
I am using this line to get a handle on the DOM element:
@ViewChild('editor') editor;
My view looks like this:
<div class="row">
<div #editor style="min-height:600px"></div>
</div>
And lastly to bootstrap it in my application:
this._editor = monaco.editor.create(this.editor.nativeElement, {
language: 'powershell'
});
As soon as the last code below is ran and the monaco editor is attached to the application, all of a sudden the number of digest cycles increases a ton. Simple mousemove events in the editor causes a change detection and I quickly see thousands of detections occur in just a few seconds. As soon as I remove the bottom lines of code the issues all go away.
The primary reason I am forced to fix this is somehow the editor here is messing with another component altogether and causing the change detection to do some weird things that in-turn causes views to re-render over a again and again. This re-rendering causes my *ngFor
loops to fail if I ever use a function to get the iterable object.
I have been poking at this issue for hours before I finally realized it was caused by the Monaco Editor. I have played around with the ChangeDetectionRef
class, but detaching the change detection doesn't seem to have any affect on the editor.
I'm fairly new to the latest versions of Angular (recently came from 1.x) so maybe I just need a special way of bootstrapping this editor so it doesn't do this. I want to be able to control the detection manually but I haven't seen that work at all so far using ChangeDetectionRef
UPDATE
Forgot to mention that I did try using ChangeDetectionStrategy.OnPush
as well to help control the change detection and it seems to have no effect at all on the underlying detection that is occurring.