Injeção inferida
Esse método de injeção requer garantir que o nome do parâmetro seja o mesmo que o nome do serviço. Se o código precisar ser comprimido, etc., a injeção falhará.
App.Controller ("myctrl1", function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}});Injeção de marcador
Esse método de injeção requer definir uma matriz de dependência, que contém o nome do serviço de dependência. Nos parâmetros da função, você pode definir o nome do parâmetro à vontade, mas a consistência do pedido deve ser garantida.
var myctrl2 = function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello (); }} myctrl2. $ injector = ['hello1', 'hello2']; App.Controller ("MyCtrl2", Myctrl2);Injeção em linha
Esse método de injeção passa diretamente em dois parâmetros, um é o nome e o outro é uma matriz. O último parâmetro dessa matriz é o corpo do método real, e o restante são todas dependências, mas é necessário garantir que os parâmetros no corpo do método estejam na mesma ordem (o mesmo que a injeção de marca).
App.Controller ("myctrl3", ['$ scope', 'hello1', 'hello2', function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}}]);Métodos comuns de $ injetor
No angular, o injetor pode ser obtido através do angular.Injector ().
var $ injector = angular.injector ();
Obtenha o nome do serviço de dependência através de $ injetor.get ('serviceName')
$ injetor.get ('$ scope')
Obtenha todas as dependências do xxx por $ injetor.annotate ('xxx')
$ injetor.annotate (xxx)
Código de amostra
<html> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <script src = "http:/paps.bdimg.com/libs/angular.js/1.2.16/angular.minjs "/libs/angular.js/1.2.16/angular.minjs" " <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'), $ injetor)); // true console.log (angular.equals ($ injector.invoke (function ($ injector) {return $ injector;}, $ injector); App.Controller ("myctrl1", function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}}); // anotado // função explícita (servicea) {}; // explícito. $ inject = ['servicea']; // $ injetor.invoke (explícito); 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 ();O exposto acima é uma compilação da informação do injetor de AngularJS. Continuaremos a adicionar informações relevantes no futuro. Obrigado pelo seu apoio a este site!