It allows for code minification. With Angular's dependency injection, the names of the variables (as the function parameters) being injected matter, and a code minifier could change them. If you provide the string versions of the variable names (in order), Angular will be able to inject the correct dependency even if the minifier changed the variable name.
If you had the original code:
.controller('MyController', function ($http) {
Then Angular's DI knows to inject the $http service. But If you run this through a code minifier, it could change your code to this:
.controller('MyController', function (a) {
Now Angular doesn't know what a
is, because it relies on the name of the variable to find the correct dependency. If you supply a string version of the dependency name, then Angular will be able to resolve it correctly even though $http
was changed to a
:
.controller('MyController', ['$http', function (a) {