admin管理员组

文章数量:1277319

I have an angular module which I want to have a dependency injected into it conditionally. i.e.

var myapp = angular.module('myapp', [
  'ngRoute',
  'myappcontroller',
  'ngGrid'              // I want to include ngGrid only if I am running a debug version of myapp
]);

Is there any way to do that?

I have an angular module which I want to have a dependency injected into it conditionally. i.e.

var myapp = angular.module('myapp', [
  'ngRoute',
  'myappcontroller',
  'ngGrid'              // I want to include ngGrid only if I am running a debug version of myapp
]);

Is there any way to do that?

Share Improve this question asked Nov 10, 2014 at 19:24 RohitRohit 7,62910 gold badges48 silver badges55 bronze badges 1
  • Almost but not quite a duplicate: stackoverflow./questions/18875714/… – Mark Amery Commented Jan 27, 2015 at 15:30
Add a ment  | 

1 Answer 1

Reset to default 13

You can, but with a bit of extra work.

The second parameter is an array so nothing prevents you from doing this:

var dev = ['foo', 'bar'];
var prod = ['foo'];
var deps = dev; //or prod


angular.module('foo', []);
angular.module('bar', []);

angular.module('myApp', deps);

本文标签: javascriptConditionally inject dependencies during angularjs module initializationStack Overflow