Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS: How to write controllers [duplicate]

I see in most examples that controllers are written this way, with an array as the second argument. And I guess the array has the dependencies listed out, then the anonymous function with the dependencies as arguments...

myApp.controller('DoubleCtrl', ['$scope', function($scope) {
    $scope.double = function(value) { return value * 2; };
}]);

and then I see sometimes they are written this way, without the array & independently listed dependencies, just the anonymous function with them as arguments.

myApp.controller('DoubleCtrl', function($scope) {
    $scope.double = function(value) { return value * 2; };
});

What's the difference? Which is better, preferred, ...?

like image 717
Gregir Avatar asked Aug 03 '26 21:08

Gregir


1 Answers

The array notation (square brackets) is used so that when your javascript code is minified angular still knows which service to inject.

If you ever plan on using javascript minification (which you should) use the square bracket notation.

like image 131
eddiec Avatar answered Aug 06 '26 12:08

eddiec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!