wrapping inputs in directives in angular

前端 未结 4 1409
广开言路
广开言路 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: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:
                    '
    \ \ \
    ' }; 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(); }); }); } });

提交回复
热议问题