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?
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
infunction($rootScope,$scope,$http)
, for example, replace it withfunction(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