There are two ways of defining Controller,module,service,and directive,
Way One:
var mymodule = angular.module (' myApp ', []); Mymodule.controller (function($scope, Project) { }]);
Way two:
var mymodule = angular.module (' myApp ', []); function Myctrl ($scope, Project) { }mymodule.controller (' Myctrl ', Myctrl);
There is no difference in nature between the two approaches, but the second one will cause the pollution of the scope.
You may also realize that the methods of dependency injection in the two definitions are somewhat different, and here is a brief summary:
1. Simple injection method (injection method)
function Myctrl ($scope, Project) {};
Mymodule.controller (' Myctrl ', Myctrl);
// or
Mymodule.controller (function($scope, Project) {
})
ANGULARJS scans the parameters of the function, extracting the name of the parameter (name) as a function dependency,
So this method requires the correctness of the parameter names, but the order of the parameters is not required;
But there is a problem with this injection method, when we publish the project to the formal environment will compress our code, then the function parameters may become a, a, this will lead to problems in our code, the following two kinds of injection methods can help us solve this problem.
2. Array notation (Array-style notation)
function ($scope, Project) { }])
Each dependent parameter value (string) is stored in an array in the same order, and the value of the array corresponds to the following function parameter one by one, so that there is no problem even if it is compressed.
If you don't like the way the array annotations are defined, here's another way.
3. Display the $inject of the calling function
ANGULARJS provides a way to notify Injector server of the dependencies you want to inject
function Myctrl (A, b) { //$scope, project, intentionally changed to a A/b after the simulated compression case = [' $scope ', ' Project '] ; Mymodule.controller (' Phonedetailctrl ', Myctrl);
As above, the effect of dependency injection can be achieved by setting the $inject property of Funciton;
Finally, there is a third-party plug-in ng-min, which can help you to the simple way to inject code into the array of comments automatically, that can satisfy your desire to write concise code, but also to avoid the compression error problem.
Ng-min Address: Https://github.com/btford/ngmin
Interested friends can study the research.