如何在加载 CKEDITOR 后将数据设置为 CKEDITOR

How to set data to CKEDITOR after CKEDITOR load?

本文关键字:CKEDITOR 数据 设置 加载      更新时间:2023-09-26

我有 HTML:

<div class="form-group">
    <textarea name="template_body">
    </textarea>
</div>
<script>
    CKEDITOR.replace('template_body');
</script>

和JS代码:

var editor = CKEDITOR.instances["template_body"];
var requestGetValue = $http({
    method: "get",
    url: "/getValue",
    dataType: 'json',
    contentType: 'application/json',
    mimeType: 'application/json'
});
requestGetValue.success(function (data) {
        editor.setData(data);
});

但有时我的data被加载到 CKEDITOR,有时没有加载。加载 CKEDITOR 后如何将数据设置为 CKEDITOR?

我认为问题出在ajax响应延迟上。

请尝试以下操作,

var editor = CKEDITOR.replace( 'template_body' );
editor.on( 'contentDom', function(){
 var requestGetValue = $http({
    method: "get",
    url: "/getValue",
    dataType: 'json',
    contentType: 'application/json',
    mimeType: 'application/json'
});
requestGetValue.success(function (data) {
        editor.setData(data);
});
});