问题
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'
});
}]);
回答1:
[...] 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`
回答2:
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
来源:https://stackoverflow.com/questions/31989675/what-is-the-meaning-of-the-square-brackets-in-modulemodulename-of-an