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, ...?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With