我试图在每次击键中捕捉摩纳哥编辑内容的价值。我尝试使用编辑器的onDidChangeModelContent()
函数,但是在playground中使用以下代码进行尝试时,这似乎不一致地触发:
const editor = monaco.editor.create(document.getElementById("container"), {
value: "function hello() {\n\talert('Hello world!');\n}",
language: "javascript"
});
editor.onDidChangeModelContent = e => {
console.log(editor.getValue());
};
当我使用Tab自动完成Intellisense建议时,仅在每次击键时都看不到控制台条目。我应该使用另一个事件侦听器,还是应该尝试其他技术?
答案 0 :(得分:0)
弄清楚了;示例代码未正确使用onDidChangeModelContent()
。要设置事件监听器,客户端代码需要 call onDidChangeModelContent()
而不是对其进行设置。这段代码有效:
editor.onDidChangeModelContent(e => {
console.log(editor.getValue());
});