angularmonaco-editorngx-monaco-editor

How to clear existing models, or is there a way to re-Init monaco editor


I'm using ngx-monaco-editor to implement a code editor in a modal. Because I need to support multiple tabs, I need to prepare a map for models to remember the models with Uris. The modal can also be reopened after closing it.

Question: Once I reopen the modal and choose a file previously opened, it will throw error:

Cannot add model because it already exists

I'm not sure why ngx-monaco-editor does not destroy the models after the component is destroyed.

Is there a way that I can manually clear those models before closing the modal.


Solution

  • Below approach is the way i find so far:

    monaco.editor.getModels().forEach(model => model.dispose());
    

    Getting all the models and calling its dispose method in component destroy method.