首页 > 解决方案 > 将数据附加到羽毛笔编辑器现有数据角度

问题描述

我正在研究一个要求,我必须将数据附加到一个容器中,该容器是 Angular 中的 quill-editor 容器。我尝试了不同的东西,但它们都不起作用。

我在下面试过:

试一试

this.mailTemplateForm.controls['body'].patchValue(value)

尝试2

this.mailTemplateForm.controls.body.setValue(value);

然后现有数据将被新数据替换。有什么解决方案。

组件.ts:

import { QuillEditorComponent } from 'ngx-quill';
export class EditMailTemplateComponent implements OnInit {
 @ViewChild('description') description: QuillEditorComponent; 
 mailTemplateForm: FormGroup;

 ngOnInit() {
  this.getFormData();
 }

 editForm(){
//console.log('test',this.data);
this.mailTemplateForm = this.fb.group({
  id: 0,
  name: [''],
  slug: [''],
  status: [''],
  subject: [''],
  body: [],
  body_parameters: [''],
 });
}

getFormData(){
  -----------
  -----------
  this.editForm();
  this.mailTemplateForm.patchValue(this.data.form_data);
}
appendTagTo(value: any){
  console.log('called - ',value);
  this.mailTemplateForm.controls.body.setValue(value); // Tried here
}

组件.html

<ul class="list-style-none mt-0">
   <li *ngFor="let field of fieldList" class="py-4 text-uppercase">
     <a color='accent' class='cursor-pointer' (click)="appendTagTo(field.field_name)"> {{ field.label_name }}
     </a>
   </li>
</ul>

<div fxFlex="75" class="mt-12">
  <quill-editor [style.display]="'block'" [style.height]="'400px'" formControlName="body" #description>
  </quill-editor>
</div>

我希望将数据添加到光标出现在 quill-editor 内的任何位置(或者如果光标在起点的编辑器中不存在)。任何帮助,将不胜感激。谢谢。

标签: angulareditorquillngx-quill

解决方案


希望这可以帮助某人。我已经通过使用注册 quill-editor 的 'onEditorCreated()' 方法解决了这个问题,如下所示:

<quill-editor [style.display]="'block'" [modules]="editorOptions.editor"
      (onEditorCreated)="onEditorCreated($event)" [style.height]="'300px'" formControlName="body" #description>
    </quill-editor>

组件.ts

public editor;

onEditorCreated(event) {
 this.editor = event;
}

appendTagTo(textTOInsert: string) {
 if(textTOInsert){
  textTOInsert = '{{'+textTOInsert+'}}';
  const selection = this.editor.getSelection(true);
  this.editor.insertText(selection.index, textTOInsert);
  //this.editor.insertHTML(selection.index, textTOInsert);
 }
}

推荐阅读