0

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 );

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

@SainulAbideen Its due to p tag, instead use span updated my answer!
Its woking.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.