-1

I just saw an example of routing in AngularJS. I would like to know the relation ship between dependency 'ngRoute' and module mainApp, in synatx var mainApp = angular.module("mainApp", ['ngRoute']);.

Previously I've seen examples with empty square brackets in module declaration.

Below is the whole context of code:

var mainApp = angular.module("mainApp", ['ngRoute']);

  mainApp.config(['$routeProvider',
     function($routeProvider) {
        $routeProvider.
           when('/addStudent', {
              templateUrl: 'addStudent.htm',
              controller: 'AddStudentController'
           }).
           when('/viewStudents', {
              templateUrl: 'viewStudents.htm',
              controller: 'ViewStudentsController'
           }).
           otherwise({
              redirectTo: '/addStudent'
           });
     }]);
jsh6303
  • 2,010
  • 3
  • 24
  • 50

2 Answers2

3

In angular, when defining a module(creating it), you pass it the names of other modules it depends on as an array (in square brackets).

In your example, the mainApp-module depends on the ngRoute-module, making the components of ngRoute(directives, services, factories, values...) available for dependency injection for the components in mainApp. To define a module that does not depend on any other modules, you pass an empty array ([]) See the angular documentation for some more info on modules

LionC
  • 3,106
  • 1
  • 22
  • 31
2

[...] defines an array

In the angular case.

mainApp is a main module( main array) and ngRoute is a sub module(like array of object).

The sample is

var ngRoute=[];//{}

var mainApp=[ngRoute];// now the `mainApp` includes the `ngRoute`
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234