本文介绍了绑定CKEDITOR值在angularjs和铁路示范文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要CKEditor的文字结合到NG-示范文本

I want to bind ckeditor text to ng-model text

我查看
    

My View

<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>


控制器

  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"];

我想在表格的时间设定在$ scope.text CKEditor的值提交

I want to set ckeditor value in $scope.text at the time of form submit

THK的提前

推荐答案

在打字时的CKEditor不更新textarea的,所以你需要照顾它。

CKEditor does not update textarea while typing, so you need to take care of it.

这里有一个指令,这将使NG-模型绑定工作,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);
      };
    }
  };
});

在HTML,只需使用:

In html, just use:

<textarea ck-editor ng-model="value"></textarea>


在previous code将在每一个变化更新NG-模式。


The previous code will update ng-model on every change.

如果您只想更新保存结合,覆盖拯救的插件,不做什么,但火救事件。

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

然后,用指令内此事件:

And then, use this event inside the directive:

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

这篇关于绑定CKEDITOR值在angularjs和铁路示范文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:44