search

Home  >  Q&A  >  body text

javascript - angularjs controller writing method

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?

为情所困为情所困2753 days ago769

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-07 09:25:58

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-07 09:25:58

    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

    reply
    0
  • Cancelreply