Rohit13
Rohit13

Reputation: 299

AngularJS controller syntax - difference between array and function version

I am new to AngularJS. What is difference between a controller declared with an array parameter, listing the dependencies both as strings and as JavaScript names,

app.controller("firstController", ['$scope', '$modal', '$log', 'HttpService', 'FisrtSharedService', 'SecondSharedService', function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService) {

}]);

...and this form, listing just the JavaScript names?

app.controller("firstController", function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService){

});

Why the weird syntax in the first version?

Upvotes: 9

Views: 3955

Answers (1)

ducdhm
ducdhm

Reputation: 1952

It's used when you minified JS files. '$scope', '$modal', '$log', 'HttpService', 'FisrtSharedService', 'SecondSharedService' just declares injectors.

app.controller("firstController", ['$scope', '$modal', '$log', 'HttpService',    'FisrtSharedService', 'SecondSharedService', function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService) {

}]);

You also declare injectors like this:

firstController.$inject = ['$scope', '$modal', '$log', 'HttpService',    'FisrtSharedService', 'SecondSharedService'];
app.controller("firstController", function($scope, $modal, $log, HttpService,  FisrtSharedService, SecondSharedService){

});

Upvotes: 12

Related Questions