npm i ng2-ace-editor
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import { NgModule ,CUSTOM_ELEMENTS_SCHEMA} from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AceEditorModule } from 'ng2-ace-editor'; import { AppComponent } from './app.component'; import { HelloComponent } from './hello.component'; @NgModule({ imports: [ BrowserModule, FormsModule,AceEditorModule ], declarations: [ AppComponent, HelloComponent ], bootstrap: [ AppComponent ], schemas:[CUSTOM_ELEMENTS_SCHEMA] }) 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 46 47 48 49 50 |
import { Component, ViewChild, AfterViewInit, OnInit } from '@angular/core'; // import 'brace/theme/github'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent implements AfterViewInit, OnInit { name = 'Angular'; str:any=''; @ViewChild('editor',{static:true}) editor; ngOnInit() { this.str = `a+b\nc+d`; console.log(); } ngAfterViewInit() { this.editor.getEditor().setOptions({ showLineNumbers: true, tabSize: 2, readOnly:false, highlightActiveLine:true, useWorker: false }); this.editor.mode = 'javascript'; this.editor.theme ='vs', this.editor.value = this.getArrayTagsHtmlString(document.getElementById('code').innerHTML).join('\t\n\t'); this.editor.getEditor().commands.addCommand({ name: "showOtherCompletions", bindKey: "Ctrl-.", exec: function (editor) { } }) // console.log(document.getElementById('code').outerHTML) } getArrayTagsHtmlString(str){ let htmlSplit = str.split(">") let arrayElements = [] let nodeElement ="" htmlSplit.forEach((element)=>{ if (element.includes("<")) { nodeElement = element+">" }else{ nodeElement = element } arrayElements.push(nodeElement) }) return arrayElements } } |
app.component.html
1 2 3 4 5 6 7 8 9 10 |
<div id="code"> <hello name="{{ name }}"></hello> <p> Start editing to see some magic happen :) </p> <div style="cursor:none !important"> <ace-editor style="height:600px;" #editor> </ace-editor> </div> </div> |
FULL SOURCE CODE