Angularjs Compression JS Technique analysis _angularjs

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.