如何将 AngularJS 数据绑定与 CKEditor 结合使用?

发布于 2024-12-11 18:57:32 字数 925 浏览 0 评论 0 原文

我该怎么办?

我能够通过使用 textarea 以及与我的模型匹配的 name 属性和带有 ng 的 script 标签将数据导入 CKEditor :bind-template 调用CKEDITOR.replace

然后我制作了一个 CKEditor 插件来检测更改并将其写回 textarea。问题在于,当 CKEditor 初始化并且 CKEditor 不拾取对 textarea 的更改时,textarea 会丢失其事件处理程序。这让我觉得我处理这个问题的方式是错误的。

接下来,我尝试使用 ng:eval-order="LAST" ng:eval="setupCKEditor()" 并通过 setupCKEditor() 函数设置编辑器。这不起作用,因为即使使用 ng:eval-order="LAST" 该函数仍然在创建节点之前运行。

我发现在 CKEDITOR.replace 周围添加 setTimeout(function () {...},0) 会有所帮助。现在唯一的问题是,当它更改模型时,它不会重新绘制屏幕,​​直到编辑另一个字段。

scope.$root.$eval(); 似乎解决了这个问题。

更新

我们最终放弃了这个,因为我们永远无法让它可靠地工作。我们切换到TinyMCE with Angular-UI一段时间,然后最终构建了一些自定义的东西。

How should I go about this?

I was able to get the data into CKEditor by using a textarea with the name attribute matching my model and a script tag with ng:bind-template to call CKEDITOR.replace.

I then made a CKEditor plugin that detects changes and writes them back to the textarea. The problem is that the textarea looses its event handlers when CKEditor initializes and CKEditor doesn't pickup changes to the textarea. This makes me think that I am approaching this the wrong way.

Next I tried using ng:eval-order="LAST" ng:eval="setupCKEditor()" and setting up the editor from the setupCKEditor() function. This didn't work because even with ng:eval-order="LAST" the function is still run before the nodes are created.

I have found that adding a setTimeout(function () {...},0) around the CKEDITOR.replace helps. Now the only problem is that when it changes the model it doesn't repaint the screen until another field is edited.

scope.$root.$eval(); seems to fix that.

Update

We ended up abandoning this since we could never get it to reliably work. We switched to TinyMCE with Angular-UI for a while and then ended up building something custom.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-12-18 18:57:32

这种方式与 onchange 插件配合使用>http://alfonsoml.blogspot.com/2011/03/onchange-event-for-ckeditor.html

angular.directive("my:ckeditor", function (expression, compiledElement) {
    return function fn(element) {
        var scope = this;

        setTimeout(function () {
            CKEDITOR.replace("editor-" + index, {extraPlugins: 'onchange'});

            scope.$watch("layers[" + index + "].src", function () {
                if (!CKEDITOR.instances["editor-" + index]) return;
                if (scope[expression] == CKEDITOR.instances["editor-" + index].getData()) return;
                CKEDITOR.instances["editor-" + index].setData(scope[expression]);
            });

            CKEDITOR.instances["editor-" + index].on("change", function () {
                scope[expression] = CKEDITOR.instances["editor-" + index].getData();
                scope.$root.$eval();
            });
        }, 0);
    };
});

更新

仅在 v0.10.6 上测试过

This sort of works with the onchange plugin from http://alfonsoml.blogspot.com/2011/03/onchange-event-for-ckeditor.html.

angular.directive("my:ckeditor", function (expression, compiledElement) {
    return function fn(element) {
        var scope = this;

        setTimeout(function () {
            CKEDITOR.replace("editor-" + index, {extraPlugins: 'onchange'});

            scope.$watch("layers[" + index + "].src", function () {
                if (!CKEDITOR.instances["editor-" + index]) return;
                if (scope[expression] == CKEDITOR.instances["editor-" + index].getData()) return;
                CKEDITOR.instances["editor-" + index].setData(scope[expression]);
            });

            CKEDITOR.instances["editor-" + index].on("change", function () {
                scope[expression] = CKEDITOR.instances["editor-" + index].getData();
                scope.$root.$eval();
            });
        }, 0);
    };
});

Update

This has only been tested on v0.10.6

べ映画 2024-12-18 18:57:32

为了完整起见,附加了一个提供角度指令的模块。我还没有使用过它,所以我无法评论它的工作/集成程度。

For completeness, attached is a module to provide an angular directive. I've not used it yet, so I can not comment on how well it works/integrates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文