wrapping inputs in directives in angular

前端 未结 4 1408
广开言路
广开言路 2021-02-07 13:46

I had the idea to wrap inputs into custom directives to guarantee a consistent look and behavior through out my site. I also want to wrap bootstrap ui\'s datepicker and dropdown

4条回答
  •  礼貌的吻别
    2021-02-07 14:39

    Here's what I believe is the proper way to do this. Like the OP I wanted to be able to use an attribute directive to wrapper an input. But I also wanted it to work with ng-if and such without leaking any elements. As @jantimon pointed out, if you don't cleanup your wrapper elements they will linger after ng-if destroys the original element.

    app.directive("checkboxWrapper", [function() {
        return {
          restrict: "A",
          link: function(scope, element, attrs, ctrl, transclude) {
            var wrapper = angular.element('
    This input is wrappered
    '); element.after(wrapper); wrapper.prepend(element); scope.$on("$destroy", function() { wrapper.after(element); wrapper.remove(); }); } }; } ]);

    And here's a plunker you can play with.

    IMPORTANT: scope vs element $destroy. You must put your cleanup in scope.$on("$destroy") and not in element.on("$destroy") (which is what I was originally attempting). If you do it in the latter (element) then an "ngIf end" comment tag will get leaked. This is due to how Angular's ngIf goes about cleaning up its end comment tag when it does its falsey logic. By putting your directive's cleanup code in the scope $destroy you can put the DOM back like it was before you wrappered the input and so ng-if's cleanup code is happy. By the time element.on("$destroy") is called, it is too late in the ng-if falsey flow to unwrap the original element without causing a comment tag leak.

提交回复
热议问题