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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -