Empty array value in angularJS module declaration

In my previous question, I understand that the code var app = angular.module('myApp', []); connects the app module to the myApp .

I would like to know why the module declaration has an empty array [] .

What is the empty array used for?

+5
source share
3 answers

This array is designed to add various modules to your current app , which is mentioned in your first part of angular.module as a string`, you could just say to inject various dependencies.

You can create the n module inside your application for each angular component, and then you can combine them into one application, and you can angular.bootstrap or apply it on the page using the ng-app directive.

Example

Suppose you have an application that has different components, such as services, factory, filters, directives, etc. You can create a module for each of them. Just to share attention.

 angular.module('myApp.filters', []) angular.module('myApp.services', []) angular.module('myApp.controllers', []) angular.module('myApp.directives', []) angular.module('myApp.constants', []) 

And adding a component to it, you can just use this particular module. As you wanted to add a service, you simply add this service to myApp.services , doing

 angular.module('myApp.services').service('example', function(){ }) 

And then inside you app.js you can combine all these modules with one module, as shown below.

 angular.module('myApp', [ 'myApp.services', 'myApp.controllers', 'myApp.directives', 'myApp.directives', 'myApp.constants' ]) 

During application initialization, you can simply use myApp inside, which will make all other modules available to it.

What is the empty array used for?

In the code, you create a module that does not introduce any dependency, [] , which means that it does not depend on any other angular module.

The dependency entered inside [] is nothing more than importing a module

+10
source

angular.module('app', []) - create a new module without any dependency on the module.

angular.module('app') - get an existing module named app .

+15
source

The angular 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.module

+6
source

All Articles