Gathering detailed insights and metrics for @gokul3710/ngx-monaco-editor-v2
Gathering detailed insights and metrics for @gokul3710/ngx-monaco-editor-v2
Gathering detailed insights and metrics for @gokul3710/ngx-monaco-editor-v2
Gathering detailed insights and metrics for @gokul3710/ngx-monaco-editor-v2
Monaco Editor component for Angular 2 and Above
npm install @gokul3710/ngx-monaco-editor-v2
Typescript
Module System
Node Version
NPM Version
TypeScript (86.69%)
JavaScript (10.97%)
HTML (1.15%)
Shell (0.88%)
CSS (0.31%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
137 Commits
1 Branches
1 Contributors
Updated on Mar 14, 2025
Latest Version
19.0.2
Package Id
@gokul3710/ngx-monaco-editor-v2@19.0.2
Unpacked Size
55.34 kB
Size
12.40 kB
File Count
12
NPM Version
10.9.2
Node Version
23.8.0
Published on
Mar 14, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
3
Using this Module you can utilize the Monaco Editor as an Angular Component. Feel free to contribute, raise feature requests and make it better.
Supports all the options available in monaco-editor Monaco Editor Options
https://ngx-monaco-editor-v2.surge.sh/
Install from npm repository:
npm install monaco-editor ngx-monaco-editor-v2 --save
Breaking change from v10, is to use monaco-editor next to ngx-monaco-editor-v2 in your package.json file.
For angular version 6 use v6.x.x
npm install ngx-monaco-editor-v2@6.0.0 --save
Add the glob to assets in angular.json
1{ 2 "apps": [ 3 { 4 "assets": [ 5 { "glob": "**/*", "input": "node_modules/monaco-editor", "output": "/assets/monaco/" } 6 ], 7 ... 8 } 9 ... 10 ], 11 ... 12}
For Angular 6 and below, add the glob to assets in .angular-cli.json
schema - projects.[project-name].architect.build
(to make monaco-editor lib available to the app):
1{ 2 "options":{ 3 {"assets": [ 4 { "glob": "**/*", "input": "node_modules/ngx-monaco-editor-v2/assets/monaco", "output": "./assets/monaco/" } 5 6 ], 7 ... 8 } 9 ... 10 }, 11 ... 12}
Include MonacoEditorModule in Main Module and Feature Modules where you want to use the editor component.(eg: app.module.ts):
1import { BrowserModule } from '@angular/platform-browser'; 2import { NgModule } from '@angular/core'; 3import { FormsModule } from '@angular/forms'; 4 5import { AppComponent } from './app.component'; 6import { MonacoEditorModule } from 'ngx-monaco-editor-v2'; 7 8@NgModule({ 9 declarations: [ 10 AppComponent 11 ], 12 imports: [ 13 BrowserModule, 14 FormsModule, 15 MonacoEditorModule.forRoot() // use forRoot() in main app module only. 16 ], 17 providers: [], 18 bootstrap: [AppComponent] 19}) 20export class AppModule { 21}
Create Editor options in component.(eg: app.component.ts)
1import { Component } from '@angular/core'; 2 3@Component({ 4 selector: 'app-root', 5 templateUrl: './app.component.html' 6}) 7export class AppComponent { 8 editorOptions = {theme: 'vs-dark', language: 'javascript'}; 9 code: string= 'function x() {\nconsole.log("Hello world!");\n}'; 10}
Include editor in html with options and ngModel bindings.(eg: app.component.html)
1<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
Include diff-editor in html with options.(eg: app.component.html)
1<ngx-monaco-diff-editor [options]="options" [originalModel]="originalModel" [modifiedModel]="modifiedModel"></ngx-monaco-diff-editor>
1import { Component } from '@angular/core'; 2import { DiffEditorModel } from 'ngx-monaco-editor'; 3 4@Component({ 5 selector: 'app-root', 6 templateUrl: './app.component.html' 7}) 8export class AppComponent { 9 options = { 10 theme: 'vs-dark' 11 }; 12 originalModel: DiffEditorModel = { 13 code: 'heLLo world!', 14 language: 'text/plain' 15 }; 16 17 modifiedModel: DiffEditorModel = { 18 code: 'hello orlando!', 19 language: 'text/plain' 20 }; 21}
To match height of container element add height: 100% and wrap in container
1<div style="height: 500px"> 2 <ngx-monaco-editor style="height: 100%" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor> 3</div>
Add class to editor tag. (eg. class="my-code-editor")
1<ngx-monaco-editor class="my-code-editor" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
Add styling in css/scss file:
1.my-code-editor { 2 .editor-container { 3 height: calc(100vh - 100px); 4 } 5}
Set automaticLayout option to adjust editor size dynamically. Recommended when using in modal dialog or tabs where editor is not visible initially.
Output event (onInit) expose editor instance that can be used for performing custom operations on the editor.
1<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor>
1export class AppComponent { 2 editorOptions = {theme: 'vs-dark', language: 'javascript'}; 3 code: string= 'function x() {\nconsole.log("Hello world!");\n}'; 4 onInit(editor) { 5 let line = editor.getPosition(); 6 console.log(line); 7 } 8}
forRoot()
method of MonacoEditorModule accepts config of type NgxMonacoEditorConfig
.
1import { NgModule } from '@angular/core'; 2import { FormsModule } from '@angular/forms'; 3import { BrowserModule } from '@angular/platform-browser'; 4 5import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor-v2'; 6import { AppComponent } from './app.component'; 7 8const monacoConfig: NgxMonacoEditorConfig = { 9 baseUrl: 'app-name/assets', // configure base path for monaco editor. Starting with version 8.0.0 it defaults to './assets'. Previous releases default to '/assets' 10 defaultOptions: { scrollBeyondLastLine: false }, // pass default options to be used 11 onMonacoLoad: () => { console.log((<any>window).monaco); } // here monaco object will be available as window.monaco use this function to extend monaco editor functionalities. 12 requireConfig: { preferScriptTags: true } // allows to oweride configuration passed to monacos loader 13 monacoRequire: (<any>window).monacoRequire // pass here monacos require function if you loaded monacos loader (loader.js) yourself 14}; 15 16@NgModule({ 17 declarations: [ 18 AppComponent 19 ], 20 imports: [ 21 BrowserModule, 22 FormsModule, 23 MonacoEditorModule.forRoot(monacoConfig) 24 ], 25 providers: [], 26 bootstrap: [AppComponent] 27}) 28export class AppModule { 29}
onMonacoLoad
property of NgxMonacoEditorConfig
can be used to configure JSON default.
1import { NgModule } from '@angular/core'; 2import { FormsModule } from '@angular/forms'; 3import { BrowserModule } from '@angular/platform-browser'; 4 5import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor-v2'; 6import { AppComponent } from './app.component'; 7 8export function onMonacoLoad() { 9 10 console.log((window as any).monaco); 11 12 const uri = monaco.Uri.parse('a://b/foo.json'); 13 monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ 14 validate: true, 15 schemas: [{ 16 uri: 'http://myserver/foo-schema.json', 17 fileMatch: [uri.toString()], 18 schema: { 19 type: 'object', 20 properties: { 21 p1: { 22 enum: ['v1', 'v2'] 23 }, 24 p2: { 25 $ref: 'http://myserver/bar-schema.json' 26 } 27 } 28 } 29 }, { 30 uri: 'http://myserver/bar-schema.json', 31 fileMatch: [uri.toString()], 32 schema: { 33 type: 'object', 34 properties: { 35 q1: { 36 enum: ['x1', 'x2'] 37 } 38 } 39 } 40 }] 41 }); 42 43} 44 45const monacoConfig: NgxMonacoEditorConfig = { 46 baseUrl: 'assets', 47 defaultOptions: { scrollBeyondLastLine: false }, 48 onMonacoLoad 49}; 50 51@NgModule({ 52 declarations: [ 53 AppComponent 54 ], 55 imports: [ 56 BrowserModule, 57 FormsModule, 58 MonacoEditorModule.forRoot(monacoConfig) 59 ], 60 providers: [], 61 bootstrap: [AppComponent] 62}) 63export class AppModule { 64}
Now pass model config of type NgxEditorModel
to Editor Component
1@Component({ 2 selector: 'app-root', 3 template: `<ngx-monaco-editor [options]="options" [model]="model"></ngx-monaco-editor>`, 4 styles: [] 5}) 6export class AppComponent { 7 options = { 8 theme: 'vs-dark' 9 }; 10 11 jsonCode = [ 12 '{', 13 ' "p1": "v3",', 14 ' "p2": false', 15 '}' 16 ].join('\n'); 17 18 model: NgxEditorModel = { 19 value: this.jsonCode, 20 language: 'json', 21 uri: monaco.Uri.parse('a://b/foo.json') 22 }; 23}
If you expose node's require
in your render process, monaco will try to use its NodeScriptLoader
and fail to load its files. To presuade it to use its BrowserScriptLoader
instead it is necessery to set preferScriptTags
to true.
1import { NgModule } from '@angular/core'; 2import { FormsModule } from '@angular/forms'; 3import { BrowserModule } from '@angular/platform-browser'; 4 5import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor-v2'; 6import { AppComponent } from './app.component'; 7 8const monacoConfig: NgxMonacoEditorConfig = { 9 baseUrl: 'assets', 10 requireConfig: { preferScriptTags: true } 11}; 12 13@NgModule({ 14 declarations: [ 15 AppComponent 16 ], 17 imports: [ 18 BrowserModule, 19 FormsModule, 20 MonacoEditorModule.forRoot(monacoConfig) 21 ], 22 providers: [], 23 bootstrap: [AppComponent] 24}) 25export class AppModule { 26}
If for some reason you want to load monaco yourself.
1<!doctype html> 2<html> 3 4<head> 5 <meta charset="utf-8"> 6 <title>Angular Electron</title> 7 <base href="./"> 8 9 <meta name="viewport" content="width=device-width, initial-scale=1"> 10 <link rel="icon" type="image/x-icon" href="assets/icons/favicon.ico"> 11</head> 12 13<body> 14 <app-root></app-root> 15 <script> 16 // Monaco uses a custom amd loader that over-rides node's require. 17 // Keep a reference to node's require so we can restore it after executing the amd loader file. 18 var nodeRequire = require; 19 </script> 20 <script src="assets/monaco/min/vs/loader.js"></script> 21 <script type="text/javascript"> 22 // Save Monaco's amd require and restore Node's require 23 var monacoRequire = require; 24 require = nodeRequire; 25 require.nodeRequire = require; 26 </script> 27</body> 28 29</html>
You just need to save monaco require
function defined in loader.js
somewhere and pass it to monacoRequire
in configuration.
1import { NgModule } from '@angular/core'; 2import { FormsModule } from '@angular/forms'; 3import { BrowserModule } from '@angular/platform-browser'; 4 5import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor-v2'; 6import { AppComponent } from './app.component'; 7 8const monacoConfig: NgxMonacoEditorConfig = { 9 baseUrl: 'assets', 10 requireConfig: { preferScriptTags: true }, 11 monacoRequire: (window as any).monacoRequire 12}; 13 14@NgModule({ 15 declarations: [ 16 AppComponent 17 ], 18 imports: [ 19 BrowserModule, 20 FormsModule, 21 MonacoEditorModule.forRoot(monacoConfig) 22 ], 23 providers: [], 24 bootstrap: [AppComponent] 25}) 26export class AppModule { 27}
Monaco Editor
Monaco Editor Options
MIT © Miroslav Maksimovic
No vulnerabilities found.
No security vulnerabilities found.