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

Rom*_*oma 12 ckeditor angularjs

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

Rom*_*oma 11

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

  ck.on('instanceReady', function() {
    ck.setData(ngModel.$viewValue);
  });
Run Code Online (Sandbox Code Playgroud)

为了完整性,这里是我使用的完整指令.

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

    }
  };
});
Run Code Online (Sandbox Code Playgroud)