angularjs - Redactor所见即所得在AngularJs指令中的集成

标签 angularjs wysiwyg directive redactor

我尝试将Beautifull WYSIWYG Redactor(http://imperavi.com/redactor/)集成到自定义AngularJS指令中。

视觉上它可以工作,但是我的自定义指令与ng-model不兼容(我不明白为什么)

这是使用我的指令的方法:

<wysiwyg ng-model="edited.comment" id="contactEditCom" content="{{content}}" required></wysiwyg>

这是指令代码:
var myApp = angular.module('myApp', []);
myApp.directive("wysiwyg", function(){

var linkFn = function(scope, el, attr, ngModel) {

    scope.redactor = null;

    scope.$watch('content', function(val) {
        if (val !== "")
        {
            scope.redactor = $("#" + attr.id).redactor({
                focus : false,
                callback: function(o) {
                    o.setCode(val);
                    $("#" + attr.id).keydown(function(){
                        scope.$apply(read);
                    });
                }
            });
        }
    });

    function read() {
        var content = scope.redactor.getCode();
        console.log(content);
        if (ngModel.viewValue != content)
        {
            ngModel.$setViewValue(content);
            console.log(ngModel);
        }
    }

};

 return {
     require: 'ngModel',
     link: linkFn,
     restrict: 'E',
     scope: {
         content: '@'
     },
     transclude: true
 };
});

最后这是 fiddle -> http://fiddle.jshell.net/MyBoon/STLW5/

最佳答案

我根据Angular-UI的TinyMCE指令制作了一个。这也听格式按钮的点击。当在指令外更改模型时,它也可以处理这种情况。

directive.coffee(对不起,coffeescript)

angular.module("ui.directives").directive "uiRedactor", ["ui.config", (uiConfig) ->

  require: "ngModel"
  link: (scope, elm, attrs, ngModelCtrl) ->
    redactor = null

    getVal = -> redactor?.getCode()

    apply = ->
      ngModelCtrl.$pristine = false
      scope.$apply()

    options =
      execCommandCallback: apply
      keydownCallback: apply
      keyupCallback: apply

    scope.$watch getVal, (newVal) ->
      ngModelCtrl.$setViewValue newVal unless ngModelCtrl.$pristine


    #watch external model change
    ngModelCtrl.$render = ->
      redactor?.setCode(ngModelCtrl.$viewValue or '')

    expression = if attrs.uiRedactor then scope.$eval(attrs.uiRedactor) else {}

    angular.extend options, expression

    setTimeout ->
      redactor = elm.redactor options
]  

html
<textarea ui-redactor='{minHeight: 500}' ng-model='content'></textarea>

关于angularjs - Redactor所见即所得在AngularJs指令中的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14280780/

相关文章:

angularjs - 如何在 Angularjs 中使用三元运算符设置 ng-attr-title?

javascript - Firebug 的脚本面板显示类似 @conn0source 的源

javascript - Ionic&AngularJS,超时未取消

javascript - TinyMCE 粘贴为纯文本

angularjs - 将对象作为属性传递给动态编译指令

javascript - limitTo 过滤前查询过滤的 ng-repeat 数据的长度

ruby-on-rails - Rails 4 WYSIWYG Bootsy 不显示格式

javascript - 为什么 document.execCommand 在reactjs中不起作用?

javascript - 如何在现有应用程序中添加 Angular js 模块

javascript - 表中自定义 html 标签的 Angular 指令