Search code examples
angularjsckeditorkeypresskeydown

Keydown event CKEditor using instance


I want get keydown event of ckeditor but for using instance in AngularJS. I create an Directive and send keydown event to my scope function. But I have many ckeditors in my page. Each keydown, I get keydown event of all my ckeditor instances.

Directive:

app.directive('ckEditor', function () {
  return {
    require: '?ngModel',
    link: function (scope, elm, attr, ngModel) {
      var ck = CKEDITOR.replace(elm[0]);
      if (!ngModel) return;

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

        editable.attachListener( ck.document, 'keydown', function(event) {
            scope.keyboard_events(event);
        });
      });

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

    }
  };
});

My Controller:

$scope.keyboard_events = function(event) {
  console.log( "agora sim ")
}

Solution

  • I solve my problem using angular-ckeditor. Much better to work with instances and ng model. (https://github.com/lemonde/angular-ckeditor)