angularjs ckeditor directive sometimes fails to load data from a service

徘徊边缘 提交于 2019-12-18 11:56:22

问题


I used Vojta's angularJS directive but sometimes ckeditor would fail to display the data from a service. This almost never happened on a refresh, but often happened when navigating back to the page. I was able to verify that the $render function was always calling ck.setData with the correct data, but sometimes it would not display.


回答1:


It appears that the $render method was sometimes called before ckeditor was ready. I was able to resolve this by adding a listener to the instanceReady event to make sure that it was called at least once after ckeditor was ready.

  ck.on('instanceReady', function() {
    ck.setData(ngModel.$viewValue);
  });

In the interest of completeness, here is the complete directive that I used.

//Directive to work with the ckeditor
//See http://stackoverflow.com/questions/11997246/bind-ckeditor-value-to-model-text-in-angularjs-and-rails
app.directive('ckEditor', function() {
  return {
    require: '?ngModel',
    link: function(scope, elm, attr, ngModel) {
      var ck = CKEDITOR.replace(elm[0],
            {
                toolbar_Full:
                [
                { name: 'document', items : [] },
                { name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
                { name: 'editing', items : [ 'Find','Replace','-','SpellChecker', 'Scayt' ] },
                { name: 'forms', items : [] },
                { name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript' ] },
                { name: 'paragraph', items : [
                'NumberedList','BulletedList','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock' ] },
                { name: 'links', items : [] },
                { name: 'insert', items : [ 'SpecialChar' ] },
                '/',
                { name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
                { name: 'colors', items : [] },
                { name: 'tools', items : [ 'Maximize' ] }
                ]
                ,
                height: '290px',
                width: '99%'
            }
    );

      if (!ngModel) return;

      //loaded didn't seem to work, but instanceReady did
      //I added this because sometimes $render would call setData before the ckeditor was ready
      ck.on('instanceReady', function() {
        ck.setData(ngModel.$viewValue);
      });

      ck.on('pasteState', function() {
        scope.$apply(function() {
          ngModel.$setViewValue(ck.getData());
        });
      });

      ngModel.$render = function(value) {
        ck.setData(ngModel.$viewValue);
      };

    }
  };
});



回答2:


   function updateModel() {
        scope.$apply(function() {
            if ( ck.getData().length ) {
                ngModel.$setViewValue(ck.getData());
            }
        });
    }

see full code:

return {
    require: '?ngModel',
    scope: true,
    link: function (scope, element, attr, ngModel) {
        if (!ngModel) return;

        var ck = CKEDITOR.replace(element[0]);

        ck.on('instanceReady', function() {
            ck.setData(ngModel.$viewValue);
        });

        function updateModel() {
            scope.$apply(function() {
                if ( ck.getData().length ) {
                    ngModel.$setViewValue(ck.getData());
                }
            });
        }

        ck.on('pasteState', updateModel);
        ck.on('change', updateModel);
        ck.on('key', updateModel);
        ck.on('dataReady', updateModel);

        ngModel.$render = function() {
            ck.setData(ngModel.$modelValue);
        };
    }
}



回答3:


I also meet this issue And when i found a new directive. It works well for me!!!

Please try this:

return {
require: '?ngModel',
scope: true,
link: function (scope, element, attr, ngModel) {
    if (!ngModel) return;

    var ck = CKEDITOR.replace(element[0]);

    ck.on('instanceReady', function() {
        ck.setData(ngModel.$viewValue);
    });

    function updateModel() {
        scope.$apply(function() {
            if ( ck.getData().length ) {
                ngModel.$setViewValue(ck.getData());
            }
        });
    }

    ck.on('pasteState', updateModel);
    ck.on('change', updateModel);
    ck.on('key', updateModel);
    ck.on('dataReady', updateModel);

    ngModel.$render = function() {
        ck.setData(ngModel.$modelValue);
    };
}

};



来源:https://stackoverflow.com/questions/15483579/angularjs-ckeditor-directive-sometimes-fails-to-load-data-from-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!