When we pass a dependency as an Array
Argument, the application does not break in production when we minify
the application.
Ways to do this :
- Using the Named function
- Using the Inline Anonymous function
Using the Named function :
We can pass dependencies as Array Arguments with the named function.
var app = angular.module('app', []);
function MyCtrl($scope) {
$scope.name = "Rohit";
};
app.controller('MyCtrl', ['$scope', MyCtrl]);
Using the Inline Anonymous function :
var app = angular.module('app', []);
app.controller('MyCtrl', ['$scope', function ($scope) {
$scope.name = "Rohit";
}]);
Differences :
The difference is that when the app.controller('mycontroller', function ($scope, myFactory, Myothers) {})
is minified, the parameter name will be minified and angular will no longer be able to figure out which dependencies to inject. The array
syntax with the dependency in a string means that it is minification
safe.
alternate solution :
we can use ng-annotate
library which will change the app.controller('mycontroller', function ($scope, myFactory, Myothers) {})
into the app.controller('mycontroller', ['$scope', 'myFactory', 'Myothers', function ($scope, myFactory, Myothers) {}])
so that the code is again minification
safe.