简体   繁体   English

angular js指令发布链接无法访问生成的元素ID

[英]angular js directive post link can't access generated element id

I have this directive: 我有这个指令:

/*html, enclosed in a ng-repeat directive*/
<textarea name="alternativaHtml" id="questao_alternativa_{{$index}}" data-ng-model="alternativa.TextoHtml" data-ck-editor></textarea>

/*javascript*/
angular
    .module("fluxo_itens.directives")
    .directive('ckEditor', [function () {
            return {
                require: '?ngModel',
                link: {
                    "post": PostLink
                }
            };
        }]);

function PostLink($scope, elm, attr, ngModel) {
    var ck = CKEDITOR.replace(attr.id);

    ck.on('pasteState', function () {
        $scope.$apply(function () {
            ngModel.$setViewValue(ck.getData());
        });
    });

    ngModel.$render = function (value) {
        ck.setData(ngModel.$modelValue);
    };
}

the problem is that when CKEDITOR tries to create the editor instance, it can't find the element, which has its id property dinamycally generated. 问题是,当CKEDITOR尝试创建编辑器实例时,它找不到该元素,该元素的id属性是动态生成的。

Just guessing , but you can try this : 只是猜测,但是您可以尝试以下操作:

<textarea name="alternativaHtml" id="questao_alternativa_{{$index}}" data-ng-model="alternativa.TextoHtml" data-ck-editor editor-id="questao_alternativa_{{$index}}"></textarea>

Directive 指示

angular
.module("fluxo_itens.directives")
.directive('ckEditor', [function () {
        return {
            scope : {
             'editorId' : '='
            }
            require: '?ngModel',
            link: {
                "post": PostLink
            }
        };
    }]);

PostLink 邮政链接

function PostLink($scope, elm, attr, ngModel) {
var ck = CKEDITOR.replace($scope.editorId);

ck.on('pasteState', function () {
    $scope.$apply(function () {
        ngModel.$setViewValue(ck.getData());
    });
});

ngModel.$render = function (value) {
    ck.setData(ngModel.$modelValue);
};
}

The problem was that CkEditor wasn't able to find the element because the id of the element was not set in the DOM. 问题在于CkEditor无法找到该元素,因为该元素的ID未在DOM中设置。 So I've relied on jQuery to set de element DOM property so the CkEditor can find the textarea. 因此,我依靠jQuery设置了元素DOM属性,以便CkEditor可以找到文本区域。 I've changed this in the PostLink function 我在PostLink函数中更改了此设置

var ck = CKEDITOR.replace(attr.id);

to this: 对此:

$(elm).attr("id", attr.id).prop("id", attr.id);
var ck = CKEDITOR.replace(elm[0].id);

Now everything works fine 现在一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM