Ironlors
Ironlors

Reputation: 193

ACE editor "Cannot read property 'getValue' of undefined"

I am currently building a markdown editor in Angular.

A codesandbox can be found here: https://codesandbox.io/embed/angular-91d27


The problem is that I am trying to get the current value in order to further work with it. The concept is that whenever the code will change (onChange()) the content is put into an Angular Service.

For testing purposes I want to print the current content onChange.

this.codeEditor.on("change", function(e) {
    const code = this.codeEditor.getValue();
    console.log(code);
});

Now whenever I am typing something I get the following error:

ERROR TypeError: Cannot read property 'getValue' of undefined

If anybody encountered this before I would be glad for some help.
Also I am wondering if this is "the right way" to handle this, or if there are better options.

Upvotes: 1

Views: 2043

Answers (1)

nash11
nash11

Reputation: 8650

Every new function defines its own this scope. Try using arrow functions instead. Arrow Functions lexically bind their context so this actually refers to the originating context.

this.codeEditor.on("change", (e) => {
    const code = this.codeEditor.getValue();
    console.log(code);
});

Upvotes: 3

Related Questions