将Ckeditor值绑定到angularjs和rails中的模型文本

swa*_*ilp 31 ruby-on-rails-3 angularjs

我想将ckeditor文本绑定到ng-model文本

我的看法

<fieldset>
<legend>Post to: </legend>
<div class="control-group">
  <label class="control-label">Text input</label>
  <div class="controls">
    <div class="textarea-wrapper">
      <textarea id="ck_editor" name="text" ng-model="text" class="fullwidth"></textarea>
    </div>
    <p class="help-block">Supporting help text</p>
  </div>
</div>

<div class="form-actions">
  <button type="submit" class="btn btn-primary">Post</button>
  <button class="btn">Cancel</button>
  <button class="btn" onclick="alert(ckglobal.getDate())">Cancel123</button>
</div>
Run Code Online (Sandbox Code Playgroud)

调节器

  function PostFormCtrl($scope, $element, $attrs, $transclude, $http, $rootScope) {
  $scope.form = $element.find("form");
  $scope.text = "";

  $scope.submit = function() {
      $http.post($scope.url, $scope.form.toJSON()).
      success(function(data, status, headers, config) {
              $rootScope.$broadcast("newpost");
              $scope.form[0].reset();
          });
  };

  $scope.alert1 = function(msg) {
      var sval = $element.find("ckglobal");
      //$('.jquery_ckeditor').ckeditor(ckeditor);                                                                                                                      
      alert(sval);
  };
}
PostFormCtrl.$inject = ["$scope", "$element", "$attrs", "$transclude", "$http", "$rootScope"];
Run Code Online (Sandbox Code Playgroud)

我想在表单提交时在$ scope.text中设置ckeditor值

提前了

Voj*_*jta 73

键入时CKEditor不会更新textarea,因此您需要处理它.

这是一个使ng-model绑定与CK协同工作的指令:

angular.module('ck', []).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());
        });
      });

      ngModel.$render = function(value) {
        ck.setData(ngModel.$viewValue);
      };
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

在html中,只需使用:

<textarea ck-editor ng-model="value"></textarea>
Run Code Online (Sandbox Code Playgroud)

以前的代码将在每次更改时更新ng-model.

如果您只想更新保存时的绑定,请覆盖"保存"插件,除了触发"保存"事件外什么都不做.

// modified ckeditor/plugins/save/plugin.js
CKEDITOR.plugins.registered['save'] = {
  init: function(editor) {
    var command = editor.addCommand('save', {
      modes: {wysiwyg: 1, source: 1},
      readOnly: 1,
      exec: function(editor) {
        editor.fire('save');
      }
    });

    editor.ui.addButton('Save', {
      label : editor.lang.save,
      command : 'save'
    });
  }
};
Run Code Online (Sandbox Code Playgroud)

然后,在指令中使用此事件:

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

      if (!ngModel) return;

      ck.on('save', function() {
        scope.$apply(function() {
          ngModel.$setViewValue(ck.getData());
        });
      });
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

  • 从ckeditor 4.2开始,应该使用'change'事件代替'pasteState'. (12认同)
  • 如果使用location.path导航,则此指令存在内存泄漏.添加它来纠正它:elm.bind('$ destroy',function(){ck.destroy();}); 此外,这里建议修复绑定问题:http://stackoverflow.com/questions/15483579/angularjs-ckeditor-directive-sometimes-fails-to-load-data-from-a-service (6认同)
  • 对于新手(像我一样),请记住 `angular.module('ck', [])` 将创建一个新模块,如果您有现有的模块,请使用 `angular.module('ck')` 因为 `[]` 会覆盖旧模块。这个简单的错误花了我大约 30 分钟,因为我不太擅长 JS 或 Angular! (2认同)

dte*_*oli 6

Vojta回答部分工作

在这篇文章中,我找到了解决方案

/sf/answers/1276545161/

最终代码:

.directive('ckEditor', function() {
                return {
                    require : '?ngModel',
                    link : function($scope, elm, attr, ngModel) {

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

                        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.$modelValue);
                        };
                    }
                };
            })
Run Code Online (Sandbox Code Playgroud)

编辑:删除未使用的括号


Gol*_*n T 5

如果您只想在角度的编辑器文本区域中检索文本,请调用CKEDITOR.instances.editor1.getData();直接在angularjs函数中获取值。见下文。

在你的HTML

在test.controller.js中

(function () {
    'use strict';

    angular
        .module('app')
            .controller('test', test);

    test.$inject = []; 

    function test() {

        //this is to replace $scope
        var vm = this;

        //function definition
        function postJob()
        {          
            vm.Description = CKEDITOR.instances.editor1.getData();
            alert(vm.Description);
        }
    } 
})();
Run Code Online (Sandbox Code Playgroud)