Search code examples
htmlangulareditorngx-quill

Unable to store the html content and display again for edit purpose


I am working on a requirement where I have to add & edit the content using quill-editor, the data is getting stored into database that is fine but after saving again I have to retrieve it for edit purpose.

My Problem:

While storing the content p tags are not adding to the content. I dont know how to fix it, I am new to quill editor & angular as well. I have searched for solution but I dint find any solution working So came here.

quill-editor HTML:

    <div>
    <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>
    <quill-editor [style.display]="'block'" (onEditorCreated)="onEditorCreated($event)" [style.height]="'400px'" formControlName="body" #description>
    </quill-editor>

component.ts

    import { QuillEditorComponent } from 'ngx-quill'; 
    export class EditMailTemplateComponent implements OnInit { @ViewChild('description') description: QuillEditorComponent; 
     public editor; 
     editForm() { 
      this.mailTemplateForm = this.fb.group({ id: 0, name: [''], body: [''], }); 
     } 
     getFormData() 
     { 
      this.editForm(); 
      this.mailTemplateForm.patchValue(this.data.form_data); 
     } 
     onEditorCreated(event) {
       this.editor = event; 
     } 
     onSubmit() 
     { 
      this.mailTemplateForm.controls.body.setValue(this.editor.getText()); 
     } 
     appendTagTo(textTOInsert: string) 
     { 
      textTOInsert = '{{'+textTOInsert+'}}'; 
      const selection = this.editor.getSelection(true); 
      this.editor.insertText(selection.index, textTOInsert); 
     } 
    }

where am I doing the mistake, Is it while storing the content into database or while retrieving the data?
Thanks for your help.


Solution

  • I have fixed the issue finally. By using innerHTML, below is the fix:

    onSubmit() {
      this.mailTemplateForm.controls.body.setValue(document.querySelector(".ql-editor").innerHTML); 
      --------
      --------
    }