Tuesday, April 30, 2013

CKEditor and AngularJs Modal boxes

There is a AngularJs timing issue when adding the ckEditor rich text editor to a page when using ng-include to load modal boxes.  The code below nicely solves this problem using a directive.

I can't take credit for writing this code, found it on http://stackoverflow.com/questions/11997246/bind-ckeditor-value-to-model-text-in-angularjs-and-rails/12021632#12021632


angular.module('yourApp').directive('ckEditor',function () {
    return {
        require: '?ngModel',
        link: function (scope, elm, attr, ngModel) {
           
            var ck = CKEDITOR.replace(elm[0],
                {
                    toolbarGroups: [
                      { name: 'clipboard', groups: ['clipboard', 'undo'] },   
                      { name: 'basicstyles', groups: ['basicstyles', 'cleanup'] }
                    ]
                }
            );
            
            if (!ngModel) return;

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

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


4 comments:

  1. Nice! This post is really useful for me and everyone. Thanks for sharing the awesome list. ROR Development Company

    ReplyDelete
  2. Really this code is very useful for developing CMS using Angular.JS

    Angularjs Training in Chennai

    ReplyDelete
  3. Really this code is very useful for developing CMS using Angular.JS

    Angularjs Training in Chennai

    ReplyDelete
  4. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!

    Offshore Angularjs Developers

    ReplyDelete