Search code examples
javascripthtmlangulartagstypescript-typings

Insert Span Element In CKEDITOR Angular


I want to insert a span element with random id in to CKEDITOR. I tried to add like this;

const viewFragment = activeEditor.editorInstance.data.processor.toView(<span id=1>name</span>);
const modelFragment = activeEditor.editorInstance.data.toModel(viewFragment );

activeEditor.editorInstance.model.insertContent(modelFragment,activeEditor.editorInstance.model.document.selection );

Solution

  • Here is a working example, using ngModel binding.

    html

    <p>This ckeditor in angular 10 :)</p>
    <ckeditor [editor]="editor" [(ngModel)]="name" [data]="data"></ckeditor>
    <button (click)="insert()">insert html</button>
    

    ts

    import { Component, VERSION } from '@angular/core';
    import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
    })
    export class AppComponent {
      name = 'Angular ' + VERSION.major;
      editor = ClassicEditor;
      data: any = `<span>Hello, world!</span>`;
    
      insert() {
        this.name += '<span>Hello, world!</span>';
      }
    }
    

    forked stackblitz