angularjs - TinyMCE <textarea> 与 AngularJS 的两种方式绑定(bind)

标签 angularjs binding tinymce

是否可以将两种方式绑定(bind)到<textarea></textarea>已应用 TinyMCE 进行富文本格式。

我无法让它工作!我可以让 TinyMCE 加载我的模型的内容,但是当我更新 TinyMCE 中的文本时,我的模型不会自动更新!

有办法吗?

最佳答案

您可以通过创建自己的指令来做到这一点。

您需要做的是,当 TinyMCE 编辑器中的某些内容发生更改时,让您的指令同步您的模型。我没有用过TinyMCE,而是用过Wysihtml5。我认为你可以重新制作它以使用 TinyMCE 代替。

angular.module('directives').directive('wysihtml5', ['$timeout',
function ($timeout) {
    return {
        restrict: 'E',
        require: 'ngModel',
        template: "<textarea></textarea>", // A template you create as a HTML file (use templateURL) or something else...
        link: function ($scope, $element, attrs, ngModel) {

            // Find the textarea defined in your Template
            var textarea = $element.find("textarea");

            // When your model changes from the outside, use ngModel.$render to update the value in the textarea
            ngModel.$render = function () {
                textarea.val(ngModel.$viewValue);
            };

            // Create the editor itself, use TinyMCE in your case
            var editor = new wysihtml5.Editor(textarea[0],
                {
                    stylesheets: ["/style.css"],
                    parserRules: wysihtml5ParserRules,
                    toolbar: true,
                    autoLink: true,
                    useLineBreaks: false,
                });

            // Ensure editor is rendered before binding to the change event
            $timeout(function () {

                // On every change in the editor, get the value from the editor (textarea in case of Wysihtml5)
                // and set your model
                editor.on('change', function () {
                    var newValue = textarea.val();

                    if (!$scope.$$phase) {
                        $scope.$apply(function () {
                            ngModel.$setViewValue(newValue);
                        });
                    }
                });

            }, 500);
        }
    };
}]);

然后您可以在 html 页面中使用该指令,如下所示:

<wysihtml5 ng-model="model.text" />

如果您需要有关创建自己的指令的更多信息,请点击以下链接:http://docs.angularjs.org/guide/directive

关于angularjs - TinyMCE &lt;textarea&gt; 与 AngularJS 的两种方式绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20005699/

相关文章:

python - 如何在 python 中将名称与多个对象或值绑定(bind)

tinymce - 使用tinymce编辑器插件正确格式化 bool 标签属性

ruby-on-rails - Rails + tinymce-rails 奇怪的错误

javascript - Webpack 不创建输出文件(不使用 webpack-dev-server)

html - 如何在 AngularJS 和 HTML 中制作滴答作响的时钟(时间)

javascript - Angular .min.js :127 Error: [$controller:ctrlreg] angularJs

javascript - firebase onAuth() 抛出 : TypeError cannot read property 'auth' of undefined

wcf - 将客户端 SSL 证书添加到 WCF 绑定(bind)

java - SLF4J 的 “dynamic binding” 功能什么时候适合使用?

html - 显示html5音频文件的src