类型错误:无法读取 AngularJS 指令中未定义的属性“childNodes”

2023-11-30

我正在 AngularJS 中制作一个指令“Tab Slide Out”,如下所示

angular.module('myApp',[]).directive('tabSlideOut', ["$window", "$document", "$timeout", function($window, $document, $timeout) {
    // default settings of a regular tab slide out
    var defaultSettings = {
        speed: 300,
        action: 'click',
        tabLocation: 'left',
        top: '200px',
        left: '50px',
        fixedPosition: true,
        positioning: 'absolute',
        onLoadSlideOut: false
    }

    // handler element
    var handler = angular.element('<a class="handler btn">{{title}}</a>');

    // panel element aka container
    var container = angular.element('<div ng-transclude></div>');

    return {
        restrict: 'E',
        transclude: true,
        replace: true,
        template: '<div class="tab-slide-out"></div>',
        scope: {
            options: "=",
            status: "=",
            title: "@"
        },
        compile: function(template) {

            // append handler to template
            template.append(handler);

            // append container to template
            template.append(container);

            console.log(template);
            // return linking function
            return function(scope, element, attrs) {
               ...
            }
        }
        
    };

如果我只使用一个,一切都会正常。但是,如果我使用 2 个或更多,则会抛出此错误类型错误:无法读取未定义的属性“childNodes”

这是 plunker 链接Demo


发生的情况是,当您添加另一个滑块时,它使用相同的滑块handler and container参考文献如第一条。由于如果元素当前存在于 DOM 中,append 实际上会移动该元素,因此它会从第一个指令中删除。

您需要为每个实例创建新元素(或克隆它们)。http://plnkr.co/edit/CC2bCXdaoAo7HjQ0oAu0?p=preview

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:无法读取 AngularJS 指令中未定义的属性“childNodes” 的相关文章

随机推荐