What is the difference between defining a controller like this
app.controller('MyCtrl', ['$scope', '$http',
function ($scope, $http) {
//...
}
]);
or this?
app.controller('MyCtrl', function ($scope, $http) {
//...
});
What is the difference between defining a controller like this
app.controller('MyCtrl', ['$scope', '$http',
function ($scope, $http) {
//...
}
]);
or this?
app.controller('MyCtrl', function ($scope, $http) {
//...
});
They both work for he same, but the array notation will survive minification.
Fist option is 'safe for minification'.
For more info please see here https://docs.angularjs.org/tutorial/step_05#a-note-on-minification
the difference is that when code is magnified the arguments passed should not be changed to other names and further code becomes in correct as we may have constructors injected into the function and other useful stuff , so in order to avoid we take arguments as array items i.e in string so they when magnified matches the arguments.
i have a good quote from a person
When you minify JavaScript the JavaScript minifier replaces the names of local variables and parameters with shorter names. However, AngularJS uses the parameter names of controller functions, factories, services and providers to decide what to inject into their factory functions. If the names are changed, AngularJS cannot inject the correct objects.
To make your AngularJS code minification safe, you need to provide the names of the objects to inject as strings. You wrap these strings in an array together with the function that needs the values injected. Here is an AngularJS minification safe dependency injection
your own example:
app.controller('MyCtrl', ['$scope', '$http',
function ($scope, $http) {
//...
}
]);