angularjs - Angular - create multiple directives from same function -
to create directive/controller/factory whatever, provide function serving "injection point":
angular.module('myapp').directive('mydirective', ['dependencies', function injectionpoint(dependencies) {}]);
can provide injectionpoint
via function registered angular? e.g. factory
? should note i've declared in separate files, , i'm trying 'the angular way' , not create unnecessary globals.
specifically, i've got 2 input directives same, different templates , isolate scope declaration.
i thought create "directive factory" use, like:
function directivefactory(scopeoverride, extrainit) { return function directiveinjectionpoint(depedencies) { return { restrict..., template..., scope: angular.extend({/*defaults*/}, scopeoverride), link: function(...) { // default stuff if(angular.isdefined(extrainit)) extrainit($scope, el, attrs); } } }
which use like:
angular.module('myapp') .directive('mydirective1', directivefactory({/* new stuff */)) .directive('mydirective2', directivefactory({/* other stuff */, fn2...));
but how register function angular, , use in directive declaration?
you can use service , directives this:
app.factory('myservice', function() { var myservice = {}; myservice.createdirective = function(scopeoverride, extrainit) { return { restrict: 'e', transclude: true, scope: scopeoverride, template: '<pre ng-transclude></pre>' }; }; return myservice; }); app.directive('drzausdirective1', function(myservice) { return myservice.createdirective(true); }); app.directive('drzausdirective2', function(myservice) { return myservice.createdirective(false); });
Comments
Post a Comment