ACE编辑器“无法读取未定义的属性'getValue'”

时间:2019-07-10 11:59:01

标签: javascript angular ace-editor

我目前正在Angular中创建markdown编辑器。

可在此处找到代码框:https://codesandbox.io/embed/angular-91d27


问题是我试图获取当前值以便进一步使用它。 这个概念是,只要代码更改(onChange()),内容就会被放入Angular Service。

出于测试目的,我想在Change上打印当前内容。

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

现在,每当我输入内容时,都会出现以下错误:

ERROR TypeError: Cannot read property 'getValue' of undefined

如果有人在遇到此问题之前,我将很高兴为您提供帮助。
我也想知道这是否是处理此问题的“正确方法”,或者是否有更好的选择。

1 个答案:

答案 0 :(得分:3)

每个新函数都定义了自己的this范围。尝试改用arrow functions。箭头函数按词法绑定它们的上下文,因此this实际上是指原始上下文。

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