做法一:
angularJs+ckeditor
一、頁面
<textarea ckeditor required name="topicContent" ng-model="topic.body" rows="20" class="form-control col-md-8 ecp-post-content" name="content"></textarea>
二、指令
app.directive('ckeditor', function() {
return {
require : '?ngModel',
link : function(scope, element, attrs, ngModel) {
var ckeditor = CKEDITOR.replace(element[0], {
});
if (!ngModel) {
return;
}
ckeditor.on('instanceReady', function() {
ckeditor.setData(ngModel.$viewValue);
});
ckeditor.on('pasteState', function() {
scope.$apply(function() {
ngModel.$setViewValue(ckeditor.getData());
});
});
ngModel.$render = function(value) {
ckeditor.setData(ngModel.$viewValue);
};
}
};
});
這樣就可以使用了,但是這樣有個bug,如果上傳圖片之后,后面不加然后字符那張圖片的標簽就不會被保存,因為圖片上傳成功后,圖片的標簽是ckeditor添加的,並不是通過我們的鍵盤或鼠標去操作的,
所以這樣ng-model就不會去執行臟檢查,如果我們的圖片是復制粘貼到上面的,就會被檢查到,ps:這里並不是真的指最后一張圖片,而是指ckeditor自動添加標簽(比如圖片上傳成功之后它會自動添加該圖片的img標簽)之后,如果我們沒有輸入,則ng-model(ng-model是自動執行臟檢查的!)的臟檢查是檢查不出來的(這里的原來具體還不清楚)
所以我最后換成了做法二,頁面使用的邏輯全部不變,只是文本編輯框不是通過ng-model去取值了,而是根據官網上的根據js/jQuery去取值
做法二、
一、頁面
<textarea required name="topicContent" ng-model="topic.body" rows="20" class="ckeditor form-control col-md-8 ecp-post-content" name="content"></textarea>
<script>
CKEDITOR.replace("content");//這里要和name屬性值一致
</script>
二、js取值
ajax提交前(angularJs就是$http提交之前)
//需要手動更新CKEDITOR字段
for ( instance in CKEDITOR.instances ){
CKEDITOR.instances[instance].updateElement();
}
然后通過
$("textarea[name='content']").val();來取值即可!!!
用方法二的方法,就解決掉了ng-model無法臟檢查ckeditor自動添加標簽的bug了(雖然是個笨方法,但是問題還是解決了!)