Angular 当模型改变时,摩纳哥三角洲生态消失在角7中
关于如何与(范围)一起使用,有以下几种方法: app.component.htmlAngular 当模型改变时,摩纳哥三角洲生态消失在角7中,angular,monaco-editor,Angular,Monaco Editor,关于如何与(范围)一起使用,有以下几种方法: app.component.html <ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor> 在ngOnInit上,一切正常。但当我更改ngModel时,代码的突出显示消失了: onFileClicked() { this.code = "this is
<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor>
在ngOnInit上,一切正常。但当我更改ngModel时,代码的突出显示消失了:
onFileClicked() {
this.code = "this is some other code'\n'
that needs to be'\n'
highlighted just like'\n'
the first one."
}
现在,当我再次更改ngModel时,旧模型的代码会在很短的时间内高亮显示,但一旦启动新模型,它就会与旧模型一起消失。新模型的代码也应该突出显示,但不是
我在这里遗漏了什么?找到了一个有效的解决方案,但我真的不知道为什么 我有一个初始化新编辑器的函数:
onFileClicked(file) {
// Angular @Input setter detects only object updates,
// so property updates are not handled =
// we need to update whole object when language changes
this.editorOptions = {
theme: "vs",
language: this.langDetect.getLanguage(file),
readOnly: true,
automaticLayout: true
};
this.code = file.data;
this.ranges = file.ranges;
}
在编辑器初始化之后,似乎必须再次手动设置模型:
onEditorInit(editor: any) {
const model = monaco.editor.getModels()[0];
monaco.editor.setModelLanguage(model, this.editorOptions.language);
model.setValue(this.code);
if (this.coverage) {
let ranges = this.buildEditorRanges(this.coverage);
editor.deltaDecorations([], ranges);
}
}
buildEditorRanges(ranges) {
let editorRanges = [];
let options = {
isWholeLine: true,
className: "code-highlight",
glyphMarginClassName: "code-highlight"
};
ranges.forEach(function(range) {
editorRanges.push({
range: new monaco.Range(
range.startLine,
range.startColumn,
range.endLine,
range.endColumn
),
options: options
});
});
return editorRanges;
}
单击文件时触发的函数中初始化编辑器之前,只需设置模型即可不起作用:
onFileClicked(file) {
this.code = file.data;
// Angular @Input setter detects only object updates,
// so property updates are not handled =
// we need to update whole object when language changes
this.editorOptions = {
theme: "vs",
language: this.langDetect.getLanguage(file),
readOnly: true,
automaticLayout: true
};
this.ranges = file.ranges;
}
我遇到了类似的问题:当我使用
Object.assign()
like动态更改theme/language
时,装饰会消失
我找到了一个解决方案:使用monaco.editor.setModelLanguage()
和monaco.editor.setTheme()
但不确定是否要更改代码内容
onFileClicked(file) {
this.code = file.data;
// Angular @Input setter detects only object updates,
// so property updates are not handled =
// we need to update whole object when language changes
this.editorOptions = {
theme: "vs",
language: this.langDetect.getLanguage(file),
readOnly: true,
automaticLayout: true
};
this.ranges = file.ranges;
}