Add directives from directive in AngularJS

后端 未结 7 1627
庸人自扰
庸人自扰 2020-11-22 10:02

I\'m trying to build a directive that takes care of adding more directives to the element it is declared on. For example, I want to build a directive that t

相关标签:
7条回答
  • 2020-11-22 10:28

    A simple solution that could work in some cases is to create and $compile a wrapper and then append your original element to it.

    Something like...

    link: function(scope, elem, attr){
        var wrapper = angular.element('<div tooltip></div>');
        elem.before(wrapper);
        $compile(wrapper)(scope);
        wrapper.append(elem);
    }
    

    This solution has the advantage that it keeps things simple by not recompiling the original element.

    This wouldn't work if any of the added directive's require any of the original element's directives or if the original element has absolute positioning.

    0 讨论(0)
提交回复
热议问题