NageswaraRao .K
NageswaraRao .K

Reputation: 53

In angularjs why we use [] in angular.module() function?

var myapp=angular.module('myAppln',[]);

I am new to angularjs. Why should we use [] in angular.module()? Could anybody please explain.

Upvotes: 0

Views: 1402

Answers (3)

brk
brk

Reputation: 50316

This is used for injecting dependency. Dependency Injection or DI is a software design pattern in which components are given their dependency instead of hard coding them within the component. This is also helpful in making dependency configurable and make components resuable & maintainable.

Components such as services & directives are defined using injectable factory For example when define a service in angularjs we do like this way

var abc = angular.module('ngAppName',[]);
    abc.factory('serviceName',['$http',function($http){
    // Rest of code goes here
    }])

angular.module('ngAppName',[]) It is more or less entry point for angular application & here we do not have any dependency so this is an empty array.

But take a look when we are defining our custom service.Here this service has dependency on $http which is predefined service offered by angular for ajax call.Our custom service(serviceName) has dependency on this $http, which we are injecting here

Another example with angular route

abc.config(['$routeProvider',function($routeProvider){
      // Rest of code goes her
    }])

The $routeProvider is what creates the $route service and it is provided by angularjs. While creating route we have to depend on $routeProvider. So we have injected it our code.

Hope this will be helpful for you.

Upvotes: 2

Alexandr Lazarev
Alexandr Lazarev

Reputation: 12882

Actually [] is an array with the dependencies that are required by the module.

Upvotes: 0

Parasmani Batra
Parasmani Batra

Reputation: 207

if you go further in Angular.js then you will read that you can pass injectors and other stuff to module. So here api return in a manner that it receaves array of options. when you wont have any you just pass it blank []

Upvotes: 0

Related Questions