What is the difference between these two angularjs controller definitions

I don't understand the difference between these two types of angularjs controller definition, I tried the following codes and found as working

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);

myApp.controller('GreetingController', function($scope) {
  $scope.greeting = 'Hola!';
});

      

+3


source to share


2 answers


You need to take care of minimization first.

In this controller:

myApp.controller('GreetingController', function($scope) {
    $scope.greeting = 'Hola!';
});

      



Arguments

will be minimized to some short values ​​and dependency injection won't work.

Take a look:

+6


source


The second one won't work after it is minified because the minifier will rename the parameters to save as much bandwidth as possible:

myApp.controller('GreetingController', function(a) {
    a.greeting = 'Hola!';
});

      

Since angular uses argument names to know what to put into the controller, this will fail.



The first syntax is a way around this problem.

I am using ngAnnotate to automatically convert the second syntax to the first as part of the build process, before minifying

+1


source







All Articles