Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果我知道id,如何使用javascript找到对对象的引用_Javascript_Angularjs_Ckeditor - Fatal编程技术网

如果我知道id,如何使用javascript找到对对象的引用

如果我知道id,如何使用javascript找到对对象的引用,javascript,angularjs,ckeditor,Javascript,Angularjs,Ckeditor,我使用了以下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> 现在设置好了,我想从指令外部的javascript向ckEditor发送一个命令。像这样: edi

我使用了以下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>
现在设置好了,我想从指令外部的javascript向ckEditor发送一个命令。像这样:

editor1.setData('xxx');

如果我知道创建它的指令是基于一个id=“editor-1”的textarea,那么如何找到ckEditor对象(例如editor1)?

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

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指令背后的原因,但我相信还有比这更优雅的解决方案

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')
    ...