npm i @materia-ui/ngx-monaco-editor
npm i monaco-editor
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; import { MonacoEditorModule, MONACO_PATH } from '@materia-ui/ngx-monaco-editor'; @NgModule({ imports: [ BrowserModule, FormsModule, MonacoEditorModule ], providers: [{ provide: MONACO_PATH, useValue: 'https://unpkg.com/monaco-editor@0.20.0/min/vs' }], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { } |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import { Component, ViewChild } from '@angular/core'; import { MonacoEditorComponent, MonacoEditorConstructionOptions, MonacoEditorLoaderService, MonacoStandaloneCodeEditor } from '@materia-ui/ngx-monaco-editor'; @Component({ selector: 'app-text-editor', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent { @ViewChild(MonacoEditorComponent, { static: false }) monacoComponent: MonacoEditorComponent; userCode: string = "var a = 2"; userLanguage: string = "javascript"; availableLanguages: string[] = [ "javascript", "html", "css" ]; userTheme: string = "vs-dark"; editorOptions: MonacoEditorConstructionOptions = { theme: this.userTheme, language: this.userLanguage, roundedSelection: true, autoIndent: true }; editor: MonacoStandaloneCodeEditor; constructor(private monacoLoaderService: MonacoEditorLoaderService) { } ngOnInit(): void { } editorInit(editor: MonacoStandaloneCodeEditor) { this.editor = editor } changeLanguage($event) { this.editorOptions = {...this.editorOptions, language: $event.currentTarget.value} } } |
app.component.html
1 2 3 4 5 6 7 8 9 |
<select name="userLanguage" class="userLanguage" [(ngModel)]="userLanguage" (change)="changeLanguage($event)"> <option *ngFor="let lang of availableLanguages" value="{{lang}}"> {{lang}} </option> </select> <ngx-monaco-editor class="textEditorContainer" [options]="editorOptions" [(ngModel)]="userCode" (init)="editorInit($event)"></ngx-monaco-editor> <textarea [innerHtml]="userCode"></textarea> |
FULL SOURCE CODE