Last active
January 30, 2016 23:12
-
-
Save Mischi/21f98127db0b2ec1a02f to your computer and use it in GitHub Desktop.
Revisions
-
Mischi revised this gist
Jan 30, 2016 . No changes.There are no files selected for viewing
-
Mischi renamed this gist
Jan 30, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Mischi revised this gist
Jan 30, 2016 . No changes.There are no files selected for viewing
-
Mischi revised this gist
Jan 30, 2016 . No changes.There are no files selected for viewing
-
Mischi revised this gist
Jan 30, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -75,8 +75,6 @@ priority: 99999, terminal: true, compile: function (tElement, tAttrs) { var lazyAttr = { for: tAttrs.lazyFor || '[ng-model]', attrs: Object.keys(tAttrs.$attr).map(function (oattr) { -
Mischi created this gist
Jan 30, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,103 @@ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>angular-lazy-attr</title> </head> <body ng-app="test"> <test-text> <lazy-attrs ng-required="v=='asdf'" ng-maxlength="10"></lazy-attrs> <lazy-attrs lazy-for="label" class="some-class-via-lazy-attr"></lazy-attrs> </test-text> <script src="node_modules/angular/angular.js"></script> <script> 'use strict'; let test = angular.module('test', ['lazyAttr']); test.directive('testText', function() { return { name: 'testText', restrict: 'E', transclude: true, template: ` <lazy-attrs-container> <ng-transclude></ng-transclude> <lazy-attrs-container-content> <div> <label for="sample"></label> <input id="sample" name="sample" type="text" ng-model="v"> {{v}} </div> </lazy-attrs-container-content> </lazy-attrs-container>`, } }); let lazyAttr = angular.module('lazyAttr', []); test.directive('lazyAttrsContainerContent', function($compile) { return { name: 'lazyAttrsContainerContent', restrict: 'E', require: '^^lazyAttrsContainer', link: function (scope, element, attrs, lazyAttrContainer) { lazyAttrContainer.lazyAttrs.forEach(function (lazyAttr) { lazyAttr.attrs.forEach(function(attr) { var forElements = element[0].querySelectorAll(lazyAttr.for); // jqLite find() only support tag selectors [].forEach.call(forElements, function(el) { el.setAttribute(attr.name, attr.value); }); }); }); $compile(element.contents())(scope); } }; }); test.directive('lazyAttrsContainer', function() { return { name: 'lazyAttrsContainer', restrict: 'E', controller: function () { this.lazyAttrs = []; } }; }); test.directive('lazyAttrs', function() { return { name: 'lazyAttrs', restrict: 'E', require: '^^lazyAttrsContainer', priority: 99999, terminal: true, compile: function (tElement, tAttrs) { // save and prefix attributes // TODO: parse "for" var lazyAttr = { for: tAttrs.lazyFor || '[ng-model]', attrs: Object.keys(tAttrs.$attr).map(function (oattr) { let attr = { name: tAttrs.$attr[oattr], value: tAttrs[oattr] }; tElement.removeAttr(attr.name); tElement.attr('lazy-prefix-' + attr.name, attr.value); return attr; }) }; return function(scope, element, attrs, lazyAttrsContainer) { lazyAttrsContainer.lazyAttrs.push(lazyAttr); } } } }); </script> </body> </html>