how to get data from CKEditor?

限于喜欢 提交于 2021-02-05 08:13:14

问题


I am trying to get the data from CKEditor? I am trying getData function but it doesnt seem to be working.

Below is the HTML

<ckeditor [editor]="Editor" id="Editor" [data]="editorData"></ckeditor>

Below is the Typescript

import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
public Editor = ClassicEditor;
ClassicEditor.create(document.querySelector('#Editor'), {
      toolbar: ['heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote'],
      heading: {
        options: [
          { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
          { model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
          { model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' }
        ]
      },
    }).then(newEditor => {
        this.Editor= newEditor;

      }).catch(error => {
        console.log(error);
    });

if I try this.Editor.getData() I am getting an error saying getData is not a function.


回答1:


this is the complete path:

1) install the ckEditor as below:

npm i ng2-ckeditor --save

2) Add the ckEditor script in the index.html:

<script src="https://cdn.ckeditor.com/4.13.0/standard/ckeditor.js"></script>

3) Add CkEditor Module to import section in AppModule like the following:

import { CKEditorModule } from 'ng2-ckeditor';

imports:
[
  BrowserModule,
  FormsModule,
  CKEditorModule
],

4) Define the following line in the top of the component

import { Component, OnInit } from '@angular/core';
declare var CKEDITOR: any;

5) Define a specific name for your ckEditor (default name is editor1): here I set content

ngOnInit(): void {
 CKEDITOR.on('instanceCreated', function (event, data) {
    var editor = event.editor,
    element = editor.element;
    editor.name = "content"
 });
}

6) in your app.component.html (add a ckEditor component and one button to get data):

<ckeditor #myEditor [(ngModel)]="ckeditorContent" [config]="{uiColor: '#a4a4a4'}" debounce="500"> </ckeditor> <input type="button" value="Get Data" (click)="getData()" />

Now, if you want to get data, use the following command:

getData() {
  console.log(CKEDITOR.instances.content.getData());
}

StackBlitz Here.

DEMO (check your browser's console)

For CKEditor Classic:

If you want to get data there are two options:

1) @ViewChild decorator

Define a @Viewchild in your component:

@ViewChild("myEditor", { static: false }) myEditor: any; 

Then in your Html:

<ckeditor #myEditor [editor]="editor" [data]="data" [(ngModel)]="data"></ckeditor>

Now, you can get data with the following code :

this.myEditor.data

2) Change Event

Import the following line in your component :

import { ChangeEvent } from "@ckeditor/ckeditor5-angular/ckeditor.component";

Define a variable in your component named retrieveddata to store data

retrieveddata: string = null;

Put the following method in your component as chagneEvent

public onChange({ editor }: ChangeEvent) {
 const data = editor.getData();
 this.retrieveddata=data;
}

Then in your Html :

<ckeditor [editor]="editor" [data]="data" [(ngModel)]="data" (change)="onChange($event)"></ckeditor>

Now, your data is stored in retrieveddata variable. console it to see.

StackBlitz Here.

DEMO (check your browser's console)




回答2:


You need to get a reference to the instance of the editor, which would be this.Editor.ckeditorInstance.getData()

https://ckeditor.com/docs/ckeditor5/latest/builds/guides/faq.html#how-to-get-the-editor-instance-object-from-the-dom-element



来源:https://stackoverflow.com/questions/58155972/how-to-get-data-from-ckeditor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!