Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of the empty array in angularJS module declaration

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?

like image 969
jsh6303 Avatar asked Aug 10 '15 16:08

jsh6303


People also ask

How do I empty an array in AngularJS?

In order to do this, there are 2 ways i.e., either use the [] notation to reinitialize the array which eventually removes all the elements from the array, or set the length of the array to 0 by using length property, which also empty the array.

How do you declare an empty array in Angular 8?

To declare an empty array for a type variable, set the array's type to Type[] , e.g. const arr: Animal[] = [] . Any elements you add to the array need to conform to the specific type, otherwise you would get an error.

What is the meaning of this Angular module myApp []) statement?

module("myApp", []); </script> The "myApp" parameter refers to an HTML element in which the application will run. Now you can add controllers, directives, filters, and more, to your AngularJS application.


1 Answers

That array is meant to add various module to your current app which is mentioned in your first part of angular.module as string`, You could simply say for injecting various dependency.

You could create an n number of module inside your app for each component of angular & then you could combine them into one single app and you can angular.bootstrap or apply it on page using ng-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.

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

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 doing

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

})

And then inside you app.js you could do combine all of this modules to an single module like below.

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

While initializing an app you could simply use myApp inside, that would make available all other module to it.

What is the empty array used for?

In you code you are creating an module which doesn't inject any dependency, [] which means it is independent of any other angular module.

Dependency injected inside the [] is nothing but importing module

like image 179
Pankaj Parkar Avatar answered Nov 08 '22 15:11

Pankaj Parkar