0

我使用了以下 HTML:

<textarea style="height: 350px; width: 100%"
   data-ck-editor="text" id="editor-1"
   data-ng-disabled="modal.action=='delete'"
   data-ng-model="modal.data.text"></textarea>

而这个指令:

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

现在设置好了,我想在指令之外从我的 javascript 向 ckEditor 发送一个命令。像这样:

editor1.setData('xxx');

知道创建它的指令是基于带有 id="editor-1" 的 textarea 时,我如何才能找到 ckEditor 对象(例如 editor1)?

4

1 回答 1

0

您可以将所有编辑器实例存储在一个服务中,该服务可以从其他任何地方访问。代码可能如下所示:

app.value("ckEditors", {});

app.directive('ckEditor', ["ckEditors", function (ckEditors) {
    return {
        require: '?ngModel',
        link: function ($scope, elm, attr, ngModel) {
            ckEditors[attr.id] = ck = CKEDITOR.replace(elm[0]);
            ...
            ...

app.controller('somectrl', function (ckEditors) {
    ckEditors["editor-1"].setData('xxx')
    ...

事件虽然我不知道您从外部指令直接访问 ckEditor 背后的原因,但我相信还有比这更优雅的解决方案。

于 2013-10-18T08:16:47.957 回答