Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally inject dependencies during angularjs module initialization

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?

like image 308
Rohit Avatar asked Nov 10 '14 19:11

Rohit


1 Answers

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);
like image 82
Tivie Avatar answered Oct 26 '22 23:10

Tivie