javascript - angularjs controller writing method
为情所困
为情所困 2017-06-07 09:23:58
0
2
741
angular.module('myApp',[])
.controller('DemoController',['$rootScope','$scope','$http',function($rootScope,$scope,$http){

}])

angular.module('myApp',[])
.controller('DemoController',function($rootScope,$scope,$http){

})

What is the difference between these two?

为情所困
为情所困

reply all(2)
phpcn_u1582

The parameters passed in after function have no order and quantity requirements.

The first way is to specify the variables passed into function using strings '$rootScope','$scope','$http', and the second way is just a simple variable name.

Because the js compression tool will confuse and compress the $rootScope,$scope,$http in function($rootScope,$scope,$http), for example, replace it with function(a,b,c) .

After the code is obfuscated and compressed, angular does not know what parameters (or dependencies) are passed into function, and it cannot run after being imported and compressed.

洪涛

These are two ways of angular dependency injection:
The first is inline injection, the second is inferred injection (there is also an explicit injection)
The difference is
Inline injection:
allows us to start from within the line when the function is defined Pass in the parameters. Furthermore, it avoids the use of temporary variables during definition.
Inferred injection:
If there is no explicit declaration, Angular will assume that the parameter name is the name of the dependency, but this process only applies to uncompressed and unobfuscated code, because Angular requires the original uncompressed parameter list for parsing. (However, you can introduce gulp-ng-annotate in the packaging process to convert inferential injection into inline injection)

It is recommended that you read the Angular dependency injection method

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template