【问题标题】:how to get data from CKEditor?如何从 CKEditor 获取数据?
【发布时间】:2020-01-29 01:04:25
【问题描述】:

我想从 CKEditor 获取数据?我正在尝试 getData 函数,但它似乎不起作用。

下面是 HTML

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

下面是打字稿

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

如果我尝试 this.Editor.getData() 我收到一条错误消息,指出 getData 不是函数。

【问题讨论】:

    标签: angular typescript ckeditor


    【解决方案1】:

    您需要获取对编辑器实例的引用,即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

    【讨论】:

    • 如果我尝试在控制台中查看 this.Editor.ckeditorInstance 的值,我会变得未定义,经典编辑器会有所不同吗?
    【解决方案2】:

    这是完整的路径:

    1) 安装ckEditor如下:

    npm i ng2-ckeditor --save
    

    2)在index.html中添加ckEditor脚本:

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

    3) 将 CkEditor 模块添加到 AppModule 中的导入部分,如下所示:

    import { CKEditorModule } from 'ng2-ckeditor';
    
    imports:
    [
      BrowserModule,
      FormsModule,
      CKEditorModule
    ],
    

    4) 在组件顶部定义以下行

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

    5) 为您的 ckEditor 定义一个特定名称(默认名称是 editor1):我在这里设置内容

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

    6) 在你的 app.component.html 中(添加一个 ckEditor 组件和一个按钮来获取数据):

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

    现在,如果要获取数据,请使用以下命令:

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

    StackBlitz Here.

    DEMO(检查浏览器控制台)

    对于 CKEditor Classic

    如果要获取数据,有两种选择:

    1) @ViewChild装饰器

    在你的组件中定义一个@Viewchild

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

    然后在你的 HTML 中:

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

    现在,您可以使用以下代码获取数据:

    this.myEditor.data
    

    2)更改事件

    在您的组件中导入以下行:

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

    在您的组件中定义一个名为retrieveddata 的变量来存储数据

    retrieveddata: string = null;
    

    将以下方法作为chagneEvent放入您的组件中

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

    然后在您的 Html 中:

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

    现在,您的数据存储在retrieveddata 变量中。安慰它看看。

    StackBlitz Here.

    DEMO(检查浏览器控制台)

    【讨论】:

    • @Jay - 我花了几个小时才知道问题出在哪里,最后,我发现了问题 - 我完全编辑了我的示例并在本地进行了测试。它就像一个魅力
    猜你喜欢
    • 2019-10-18
    • 2018-03-30
    • 1970-01-01
    • 2018-10-15
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2012-05-17
    相关资源
    最近更新 更多