AngularJS dependency injection order

I am working through an ebook to find out the whole MEAN stack and am facing an odd problem while working with angular.

Specifically, I was adding angular-route to my template rendering app. I couldn't get it to work at first and revisited the code several times looking for any mistake I might have made. In the end, I typed the order of the two dependencies for the main application module differently than the book showed.

It didn't work

var mainApplicationModule = angular.module(mainApplicationModuleName, ['example', 'ngRoute']);

      

It worked

var mainApplicationModule = angular.module(mainApplicationModuleName, ['ngRoute', 'example']);

      

So, I don't have a problem for sure, but I was wondering if someone could explain why it works like this? I haven't been able to find anything about how important it is to deal with dependency definition. I can post more of my code if it would be helpful.

+3


source to share


1 answer


It comes from Brad Daley's book on the topic. The order is that the list of modules (dependencies) to be introduced must be in "required" order. So if a module example

requires ngRoute

, then there ngRoute

should be a before example

.

The angular.module () method uses the following syntax:

angular.module(name, [requires],[configFn])



The name parameter is the name in which the module is registered with the injector service. The require parameter is an array of module names that are added to the injector service to use that module.

This explanation answers the question about the possibility of circular module references in a more complex case. Here is some information on what David M. Carr SO Response Link .

+2


source







All Articles