angularjs - Is there any better way for creating by code a directive object? -
angular.element('<my-directive></my-directive>')
why can not name of directive ? seems strange write html line inside js...
any better way creating instance of directive in memory?
a common pattern writing directives this.
angular.module('myapp.directives', ['myapp.services']) /* toggleclass generic directive toggling class directly on element or specifying toggle-target option eg. toggleclass="{class: 'open', target='.element'}" */ .directive('toggleclass', function(){ return { restrict: 'a', link: function(scope, element, attrs){ element.on('click', function(){ var ops = scope.$eval(attrs.toggleclass); // if toggle-target set if(ops.target){ element = angular.element(document.queryselector(ops.target)); } // toggle class element.toggleclass(ops.class); }); } } }); });
Comments
Post a Comment