2014-12-22 24 views
0

我有HTML:如何在CKEDITOR加載後將數據設置爲CKEDITOR?

<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?

+0

嘗試使用異步:假的。 –

回答

2

我認爲問題是與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); 
}); 

}); 
+0

如果我使用**。on()**,則請求** requestGetValue **每次發送 – TestUser

+1

然後,使用setTimeout來查找編輯器實例是否準備就緒。如果準備就緒,則調用setData並清除時間間隔。 – Kirubachari

相關問題