Angularjs dependency injection parameter

61 views Asked by At

What is the difference of the below code

.factory('Service', ['$log', function($log) {}]);

.factory('Service', function($log) {});

both are working fine in my app.

2

There are 2 answers

0
Andy Gaskell On BEST ANSWER

The first one is considered safer when minified. I generally don't have a problem with the second version with my current minifier though.

Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

0
Nader Dabit On

The first factory with the added array, is used when the code is going to be minified to stop the code from breaking once it is minified. If you are not planning on minifying your code, either will work. If you are going to be minifying it, then go with the array syntax.