How to dynamically inject modules and services into app and controllers in AngularJS? -
i have app decides if needs specific set of modules inside of application run block. (removed unnecessary logic)
angular.module('myapp', []).run(function () { if (true) { //needs have modules injected myapp } });
how can add modules myapp, after angular app has initiated?
once module injected dynamically, how can use injected module's services in other places of app, controllers , directives?
angular.module('myapp').controller(function ($scope, myservice) { //where myservice belongs dynamically added module myservice.dostuff(); //this give me error if module wasn't injected });
i know it's possible add modules inline array annotation, checking if myservice exists, inside of controllers. i'm trying correct way, instead of having check myservice, thought-out whole code base.
i appreciate help!
you can explicitly bootstrap application module after determining need.
var modulestoinject = ["mydep1"]; // push more modules here needed angular.module("myapp", modulestoinject); angular.bootstrap(document,["myapp"]);
Comments
Post a Comment