ace.edit can't find div #javascript-editor in mat-tab

Viewed 1209

I'm trying to create an ace editor which is throwing an error when used inside angular material

ace.edit can't find div #javascript-editor

I have my code here StackBlitz (check console for the error)

app.component.html

<mat-tab-group>
  <mat-tab label="Editor">
    <h4>Custom Editor</h4>
    <div id="javascript-editor" style="height: 300px;"></div>
  </mat-tab>
</mat-tab-group>

app.component.ts

import { Component, OnInit } from '@angular/core';

import * as ace from 'brace';
import 'brace/mode/javascript';
import 'brace/theme/monokai';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  ngOnInit() {
    const editor = ace.edit('javascript-editor');
    editor.getSession().setMode('ace/mode/javascript');
    editor.setTheme('ace/theme/monokai');
  }
}
1 Answers

You are calling ace.edit when the javascript-editor element is not created yet, try calling it from ngAfterViewInit instead of ngOnInit.

Related