angular.js - angularJS controller 第二个参数是什么意思?
滿天的星座
滿天的星座 2017-05-15 17:04:50
0
3
568
 var app = angular.module('app',['ngPlugin']);
    app.controller('frontTourism',['$scope','$http','stModal','$timeout','ngSubmit','alert',function(){/*省略*/}])

'$scope','$http','stModal','$timeout','ngSubmit','alert' 这些有什么用?

滿天的星座
滿天的星座

reply all(3)
过去多啦不再A梦

The meaning of dependency injection, let me explain it in detail.

An object usually has three ways to gain control over its dependencies:
(1) Create dependencies internally;
(2) Reference through global variables;
(3) Pass parameters where needed.
Angular’s ​​dependency injection is implemented through the third way. The remaining two methods will bring various problems, such as polluting the global scope and making isolation extremely difficult.
From a functional point of view, dependency injection will automatically find dependencies in advance and inform the injection target of the dependent resources, so that the resources can be injected immediately when the target needs it.

Angular has 3 injection methods:
a, inferred injection
app.controller('MyCtrl', function($scope) {
});

b, annotated injection
var myFunc=function($scope) {
});
myFunc.$inject = ['$scope'];
app.controller('MyCtrl',myFunc);

c, inline injection
app.controller('MyCtrl', ['$scope', function($scope) {
}]);

The first method is based on the written parameter name, such as $scope, and calls $inject internally to inject $scope into the dependency. If a compression tool is used in front-end development, $scope will be changed into another letter, and it will not be possible. Inference has been made, and for the other two methods, you can change function($scope) to function(a). It doesn’t matter;
The second method requires writing one more line of code;
It is generally recommended to use the third method.

巴扎黑

These are objects used for injection. Only by injecting these objects can they be used in the following functions
$scope is required for page value transfer
$http is required for http requests

. . .

为情所困

Module injection, introduce other modules you need to use.
For example, $http is a module

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