简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM