Angularjs 与Ckeditor

 

Angularjs 诞生于Google是一款优秀的前端JS框架,已经被用于Google的多款产品当中。AngularJS有着诸多特性,最为核心的是:MVC、模块化(Module机制)、自动化双向数据绑定、语义化标签(Directive)、依赖注入、路由(Route)机制、服务(services)机制等等。之前也用过Jquery、Extjs等,如今刚开始接触AngularJS,感受这是一个很吸引人的一个框架。javascript

学习网址:html

咱们在作B/S系统是常常会用到在线编辑器(FreeTextBox,Ckeditor,KindEditor等等),我比较经常使用的是kindEditor和ckEditor。前端

开始打算用kindEditor,java

mainApp.directive('kindeditor', function() {
    return {
        require : '?ngModel',
        link : function(scope, element, attrs, ngModel) {
            var editor;
            KindEditor.ready(function(K) {
                editor = K.create(element[0], {
                    resizeType : 1,
                    allowPreviewEmoticons : false,
                    allowImageUpload : false,
                    items : [
                        'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                        'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                        'insertunorderedlist', '|', 'emoticons', 'image', 'link']
                });
            });
            if (!ngModel) {
                return;
            }
            editor.on('instanceReady', function() {
                editor.setData(ngModel.$viewValue);
            });
            editor.on('pasteState', function() {
                scope.$apply(function() {
                    ngModel.$setViewValue(editor.getData());
                });
            });
            ngModel.$render = function(value) {
                editor.setData(ngModel.$viewValue);
            };
        }
    };
});

不过没有成功,缘由使Editor 须要在KindEditor.ready中初始化,后面的editor为undefined,因此edito.on()等没法运行,这个地方可能会有其余方法,可是我暂时没有找到方法,若是有朋友找到,能够交流下。angularjs

而后有使用了ckeditor写了一个指令app

/**
 * ckeditor Directive
 * @author 张世民 @ 数云图
 */
mainApp.directive('ckeditor', function() {
    return {
        require : '?ngModel',
        link : function(scope, element, attrs, ngModel) {
            var ckeditor = CKEDITOR.replace(element[0], {
                
            });
            if (!ngModel) {
                return;
            }
            ckeditor.on('pasteState', function() {
                scope.$apply(function() {
                    ngModel.$setViewValue(ckeditor.getData());
                });
            });
            ngModel.$render = function(value) {
                ckeditor.setData(ngModel.$viewValue);
            };
        }
    };
});


这样能够成功使用了。框架

可是在编辑时又发现问题了,在第二次编辑时没有执行编辑器

ckeditor.setData(ngModel.$viewValue);

 又给ckeditor绑定了instanceReady事件,这样用起来比较完美了,最后ckeditor指令以下模块化

/**
 * ckeditor Directive
 * @author 张世民 @ 数云图
 */
mainApp.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);
            };
        }
    };
});


用法简单,只须要在html标签上加入ckeditor 指令学习

<textarea ckeditor ng-model="entity.catalog" class="form-control"></textarea>

 补充:

几位朋友说Ueditor挺好用的,测试了一下,写了一个AngularJs的Ueditor指令

mainApp.directive('ueditor', function() {
    return {
        require : '?ngModel',
        link : function(scope, element, attrs, ngModel) {
            var ueditor = UE.getEditor(element[0], {
                //配置
            });
            if (!ngModel) {
                return;
            }
            ueditor.on('instanceReady', function() {
                ueditor.setContent(ngModel.$viewValue);
            });
            ueditor.on('pasteState', function() {
                scope.$apply(function() {
                    ngModel.$setViewValue(ueditor.getContent());
                });
            });
            ngModel.$render = function(value) {
                ueditor.setContent(ngModel.$viewValue);
            };
        }
    };
});

用法只需在Html标签上加上ueditor指令

<textarea ueditor ng-model="entity.catalog" class="form-control"></textarea>
相关文章
相关标签/搜索