An inferred injection
This injection method requires that the parameter name be the same as the service name. If the code is compressed and so on, it can cause the injection to fail.
App.controller ("MyCtrl1", Function ($scope, Hello1,hello2) {
$scope. Hello = function () {
Hello1.hello ();
Hello2.hello ();
}
);
Tagged injection
This injection way, you need to set a dependent array, the array is dependent on the service name, in the function parameters, you can set the parameter name, but must ensure the consistency of the order.
var myCtrl2 = function ($scope, Hello1,hello2) {
$scope. Hello = function () {
Hello1.hello ();
Hello2.hello ();
}
MyCtrl2. $injector = [' Hello1 ', ' Hello2 '];
App.controller ("MyCtrl2", MYCTRL2);
Inline injection
This injection method passes directly to two parameters, one is the name and the other is an array. The last argument to this array is the real method body, and the rest is the dependent target, but it is guaranteed to be in the same order as the method body's parameters (as with tag injection).
App.controller ("MyCtrl3", [' $scope ', ' hello1 ', ' Hello2 ', function ($scope, Hello1,hello2) {
$scope. Hello = function () {
Hello1.hello ();
Hello2.hello ();
}
]);
$injector Common methods
In angular, the injector can be obtained by Angular.injector ().
var $injector = Angular.injector ();
Access to dependent service names via $injector.get (' ServiceName ')
$injector. Get (' $scope ')
Get all the dependencies for XXX through $injector.annotate (' xxx ')
$injector. Annotate (XXX)
Sample code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl1">
<input type="button" ng-click="hello()" value="ctrl1"></input>
</div>
<div ng-controller="myCtrl2">
<input type="button" ng-click="hello()" value="ctrl2"></input>
</div>
<div ng-controller="myCtrl3">
<input type="button" ng-click="hello()" value="ctrl3"></input>
</div>
<script type="text/javascript">
var app = angular.module("myApp",[]);
app.factory("hello1",function(){
return {
hello:function(){
console.log("hello1 service");
}
}
});
app.factory("hello2",function(){
return {
hello:function(){
console.log("hello2 service");
}
}
});
var $injector = angular.injector();
console.log(angular.equals($injector.get('$injector'),$injector));//true
console.log(angular.equals($injector.invoke(function($injector) {return $injector;}),$injector));//true
//inferred
// $injector.invoke(function(serviceA){});
app.controller("myCtrl1", function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
});
//annotated
// function explicit(serviceA) {};
// explicit.$inject = ['serviceA'];
// $injector.invoke(explicit);
var myCtrl2 = function($scope,hello1,hello2){
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
}
myCtrl2.$injector = ['hello1','hello2'];
app.controller("myCtrl2", myCtrl2);
//inline
app.controller("myCtrl3",['$scope','hello1','hello2',function($scope,hello1,hello2){
// app.controller("myCtrl3",['$scope','hello1','hello2',function(a,b,c){
// a.hello = function(){
// b.hello();
// c.hello();
// }
$scope.hello = function(){
hello1.hello();
hello2.hello();
}
}]);
console.log($injector.annotate(myCtrl2));//["$scope","hello1","hello2"]
</script>
</body>
</html>
Above is the ANGULARJS injector data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!