Search code examples
javascriptangularjsdependency-injectionangularjs-module

How to fix an angular.module not working with dependent modules?


I'm pretty clear on how angular.module works, but this eludes me for whatever reason.

In my code I have the following

var app = angular.module("myApp", [])

app.controller("MainCtrl", ...)

but my code only works when there is no array for dependent modules, like this:

     var app = angular.module("myApp"); 
     app.controller("MainCtrl",...)

I haven't the slightest the clue as to what would cause the problem, as I've always understood the need for the empty array.


Solution

  • I've always understood the need for the empty array

    Array as the second argument is only needed when the module is created. So with

    angular.module("myApp", [])
    

    you create new module. And with

    angular.module("myApp") 
    

    you retrieve already existent module which was previously created.

    my code only works when there is no array for dependent modules

    It means that you have already created module myApp. In this case you don't have to recreate it again, as it will erase all previously registered controllers, etc.