In my previous question, I understand that the code var app = angular.module('myApp', []);
connects the module app
to the view myApp
.
I would like to know why we are having the empty array []
in the module declaration.
What is the empty array used for?
Angulars API says
Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module
https://docs.angularjs.org/api/ng/function/angular.moduleangular.module('app', [])
is to create a new module without any module dependency.angular.module('app')
is to retrieve an existing module whose name isapp
.You could create an
n
number of module inside your app for each component ofangular
& then you could combine them into one single app and you canangular.bootstrap
or apply it on page usingng-app
directive.Example
Suppose you have an app which has different component like services, factory, filters, directives, etc. You could create a module for each of them. Just for making separation of concern.
And while appending an component to it you could simply use that specific module of it. Like you wanted to add service then you just add that service into
myApp.services
by doingAnd then inside you app.js you could do combine all of this modules to an single module like below.
While initializing an app you could simply use
myApp
inside, that would make available all other module to it.In you code you are creating an module which doesn't inject any dependency,
[]
which means it is independent of any otherangular
module.