wrapping inputs in directives in angular

前端 未结 4 1407
广开言路
广开言路 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:25

    Why not wrap the input in the compile function? The advantage is that you will not have to copy attributes and will not have to cleanup in the scope destroy function. Notice that you have to remove the directive attribute though to prevent circular execution.

    (http://jsfiddle.net/oscott9/8er3fu0r/)

    angular.module('directives').directive('wrappedWithDiv', [
        function() {
            var definition = {
                restrict: 'A',
                compile: function(element, attrs) {
                    element.removeAttr("wrapped-with-div");
                    element.replaceWith("<div style='border:2px solid blue'>" +
                        element[0].outerHTML + "</div>")
                }
            }
            return definition;
        }
    ]);
    
    0 讨论(0)
  • 2021-02-07 14:32

    Why not doing a directive like that?

    myApp.directive('wrapForm', function(){
        return {
            restrict: 'AC',
            link: function(scope, inputElement, attributes){                       
                var overallWrap = angular.element('<div />');
                var validation = angular.element('<div />').appendTo(overallWrap);
                var button = angular.element('<div />').appendTo(overallWrap);
                var inputWrap = angular.element('<div />').appendTo(overallWrap);
    
                overallWrap.insertBefore(inputElement);
                inputElement.appendTo(inputWrap);
    
                inputElement.on('keyup', function(){
                    if (inputElement.val()) {
                        validation.text('Just empty fields are valid!');
                    } else {
                        validation.text('');
                    }
                });            
            }
        }
    });
    

    Fiddle: http://jsfiddle.net/bZ6WL/

    Basically you take the original input field (which is, by the way, also an angularjs directive) and build the wrappings seperately. In this example I simply build the DIVs manually. For more complex stuff, you could also use a template which get $compile(d) by angularjs.

    The advantage using this class or html attribute "wrapForm": You may use the same directive for several form input types.

    0 讨论(0)
  • 2021-02-07 14:38

    Based on this: http://angular-tips.com/blog/2014/03/transclusion-and-scopes/

    This directive does transclusion, but the transcluded stuff uses the parent scope, so all bindings work as if the transcluded content was in the original scope where the wrapper is used. This of course includes ng-model, also min/max and other validation directives/attributes. Should work for any content. I'm not using the ng-transclude directive because I'm manually cloning the elements and supplying the parent(controller's) scope to them. "my-transclude" is used instead of ng-transclude to specify where to insert the transcluded content.

    Too bad ng-transclude does not have a setting to control the scoping. It would make all this clunkyness unnecessary. And it looks like they won't fix it: https://github.com/angular/angular.js/issues/5489

        controlsModule.directive('myWrapper', function () {
            return {
                restrict: 'E',
                transclude: true,
                scope: {
                    label: '@',
                    labelClass: '@',
                    hint: '@'
                },
    
                link: link,
                template:
                    '<div class="form-group" title="{{hint}}"> \
                        <label class="{{labelClass}} control-label">{{label}}</label> \
                        <my-transclude></my-transclude> \
                     </div>'
            };
    
            function link(scope, iElement, iAttrs, ctrl, transclude) {
    
                transclude(scope.$parent,
                    function (clone, scope) {
    
                        iElement.find("my-transclude").replaceWith(clone);
    
                        scope.$on("$destroy", function () {
                            clone.remove();
                        });
                    });
            }
        });
    
    0 讨论(0)
  • 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('<div class="wrapper">This input is wrappered</div>');
    
            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.

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