directives.directive('ueditor', [ 'Common', '$rootScope', function(Common, $rootScope) { return { restrict: 'EA', require: 'ngModel', link: function(scope, ele, attrs, ctrl) { $rootScope.$emit('loading', '初始化編輯器...');//廣播loading事件,可以刪除 var _self = this, _initContent, editor, editorReady = false, base = '/public/vendor/utf8_qiniu_ueditor-master', //ueditor目錄 _id = attrs.ueditor; var editorHandler = { init: function() { window.UEDITOR_HOME_URL = base + '/'; var _self = this; if (typeof UE != 'undefined') { editor = UE.getEditor(_id, { toolbars: [ [ 'fontsize', '|', 'blockquote', 'horizontal', '|', 'removeformat', '|', 'insertimage', '|', 'bold', 'italic', 'underline', 'forecolor', 'backcolor', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'insertorderedlist', 'insertunorderedlist', '|', 'link', 'unlink', '|', 'emotion' ] ] }); editor.ready(function() { editor.setHeight(500); editorReady = true; $rootScope.$emit('loading', '');//編輯器初始化完成 editor.addListener('contentChange', function() {//雙向繫結 if (!scope.$$phase) { scope.$apply(function() { ctrl.$setViewValue(editor.getContent()); }); } }); }); } else { Common.loadScript(base + '/ueditor.config.js', null); Common.loadScript(base + '/ueditor.all.min.js', function() { _self.init(); }); } }, setContent: function(content) { if (editor && editorReady) { editor.setContent(content); } } }; ctrl.$render = function() { _initContent = ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue; editorHandler.setContent(_initContent);//雙向繫結 }; editorHandler.init(); //事件 $rootScope.$on('$routeChangeStart', function() { editor && editor.destroy(); }); } } } ]); |