AngularJs整合百度ueditor編輯器教程

來源:互聯網
上載者:User

我們先建立一個addScript函數,用於動態載入js指令碼。


/**
 * 動態載入js檔案檔案
 * @param url
 * @param callback
 */
function addScript(url,callback){
    var elt = document.createElement("script");
    elt.src = url;
    elt.anysc = true;
    if(elt.onload==null){
        elt.onload = function(){
            typeof callback=='function'&&callback();
        }
    }else{
        elt.onreadystatechange = function(){
            if(elt.readyState=="loaded" || elt.readyState=="complete"){
                typeof callback=='function'&&callback();
            }
        }
    }
    document.getElementsByTagName("body")[0].appendChild(elt);
}

然後寫angularjs指令

directivesApp.directive('ueditor',
    function () {
        return{
            restrict: 'EA',
            require: 'ngModel',
            scope: {
                height: '@?'
            },
            link: function (scope, element, attr, ctrl) {
                var _self = this,
                    _initContent,
                    editor,
                    editorReady = false,
                    baseURL = "/assets/vendor/jquery_plugin/ueditor/1.4.3/"; //寫你的ue路徑
 
                var fexUE = {
                    initEditor: function () {
                        var _self = this;
                        if (typeof UE != 'undefined') {
                            editor = new UE.ui.Editor({
                                initialContent: _initContent,
                                toolbars: [
                                    ['source', 'undo', 'redo', 'bold', 'italic',  'removeformat', 'formatmatch', 'autotypeset', 'blockquote',
                                        'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist']
                                ],
                                initialFrameHeight:scope.height || 120,
                                autoHeightEnabled:false,
                                wordCount:false,
                                elementPathEnabled: false
                            });
 
 
                            editor.render(element[0]);
                            editor.ready(function () {
                                editorReady = true;
                                _self.setContent(_initContent);
 
                                editor.addListener('contentChange', function () {
                                    scope.$apply(function () {
                                        ctrl.$setViewValue(editor.getContent());
                                    });
                                });
                            });
                        } else {
 
                            addScript(baseURL + 'ueditor.config.js');
                            addScript(baseURL + 'ueditor.all.min.js', function(){
                                _self.initEditor();
                            })
                        }
                    },
                    setContent: function (content) {
                        if (editor && editorReady) {
                            editor.setContent(content);
                        }
                    }
                };
 
                /**
                 * 當Model改變值得時候賦值。
                 */
                ctrl.$render = function () {
                    _initContent = ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue;
                    fexUE.setContent(_initContent);
                };
 
                fexUE.initEditor();
            }
        }
    }
)


好了,ue的封裝就完成了。

關於ue的寬度不能100%的問題,我在我項目中css中寫了如下:


/*修證百度的寬度100%問題*/

#edui1{width:100% !important; margin-bottom: 10px;}/* 這個ID為編輯器的ID */
#edui1_iframeholder{width:100% !important}/* 這個ID為編輯器可編輯區域的ID */

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.