- 在HTML模板中添加maxlength属性:
- 在组件中设置最大长度和实时获取编辑器内容长度:
import { Component, ViewChild } from '@angular/core';
import { QuillEditorComponent } from 'ngx-quill';
@Component({
selector: 'app-editor',
templateUrl: './editor.component.html',
styleUrls: ['./editor.component.scss'],
})
export class EditorComponent {
@ViewChild('editor') editor: QuillEditorComponent;
editorContent = '';
maxEditorLength = 5000;
get editorLength(): number {
return this.editor?.quill?.getLength() || 0;
}
}
- 在保存操作时检查是否超出最大长度:
saveContent() {
if (this.editorLength > this.maxEditorLength) {
// 超出最大长度限制
return;
}
// 保存操作
}