This article illustrates the operation skills of Angularjs compression JS. Share to everyone for your reference, specific as follows:
Most Web projects in the release of the JS code will be compressed, in order to reduce the size of JS file, save a little traffic.
It is simply the principle of renaming parameters and part of variable names and functions.
But there are exceptions to this type of work in ANGULARJS applications.
Since Angularjs's dependency injection is injected according to the parameter name, it is clear that renaming the parameter destroys the mechanism.
If you do not do special processing, compression (minify), this error will occur at execution time
Unknow provider:aprovider<-a
The official explanation for this error is that no dependent service is found
In other words, this dependency injection can cause errors.
Fortunately, Angularjs has built a standard mechanism to deal with this problem.
The simplest and most common way is to use arrays instead of functions. such as:
. Controller (' Registerctrl ', [' $scope ', ' $interval ', ' $timeout ', function ($scope, $interval, $timeout) {
//do Something
}]);
The last element of the array is always a function, and the previous arguments are strings, corresponding to the parameter one by one in this function.
Another form is the so-called annotation way. as
var Objctrl = function ($scope, $timeout, $interval) {
//do something
}
//Add a $inject property to the Objctrl function, which is an array, Defines the objects that need to be injected
objctrl. $inject = [' $scope ', ' $interval ', ' $timeout '];
The dependency injection form here is not limited to controller, and everything that requires DI (directive, factory, services, etc.) can be used in both ways.
I hope this article will help you to Angularjs program design.