angularjs - Use angular compileProvider outside config block -
i'm trying create directives on fly, achived that, seams pretty hacky.
this first approach:
function create(mydir) { angular.module("app").directive(mydir.name, function() { return { template:mydir.template }; }); }
it didn't work because can't register directives after application started.
based on post: http://weblogs.thinktecture.com/pawel/2014/07/angularjs-dynamic-directives.html
i found out use compileprovider work, since compileprovider isn't available outside config block, need put out, did:
var provider = {}; angular.module("app",[]); angular.module('app') .config(function ($compileprovider) { //it feels hacky me too. angular.copy($compileprovider, provider); }); .... function create(mydir) { provider.directive.apply(null, [mydir.name, function () { return { template: mydir.template } }]); render(mydir); //this render new instance of new directive }
surprisingly worked. can't feel being hacking compileprovider, because i'm using not in way suppose be, know if possible use compileprovider after application has started.
there list of dependencies can injected config
blocks (these built-in $provide
, $injector
, service providers) , list of dependencies can injected everywhere else (service instances , old $injector
). can see constant
adding dependency both lists.
a common recipe using providers outside config
is
app.config(function ($provide, $compileprovider) { $provide.constant('$compileprovider', $compileprovider); });
Comments
Post a Comment