angularjs ckeditor指令有时无法从服务加载数据

时间:2013-03-18 17:53:25

标签: angularjs ckeditor

我使用Vojta's angularJS directive但有时ckeditor无法显示服务中的数据。这几乎从未在刷新时发生,但经常在导航回页面时发生。我能够验证$ render函数始终使用正确的数据调用ck.setData,但有时它不会显示。

3 个答案:

答案 0 :(得分:11)

似乎在ckeditor准备好之前有时会调用$ render方法。我能够通过向instanceReady事件添加一个监听器来解决这个问题,以确保在ckeditor准备就绪后至少调用它一次。

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

为了完整起见,这是我使用的完整指令。

//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);
      };

    }
  };
});

答案 1 :(得分:3)

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

参见完整代码:

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);
        };
    }
}

答案 2 :(得分:0)

我也遇到了这个问题 当我找到一个新的指令。它适用于我!!!

请试试这个:

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);
    };
}

};