Inyección inferida
Este método de inyección requiere garantizar que el nombre del parámetro sea el mismo que el nombre del servicio. Si el código necesita ser comprimido, etc., la inyección fallará.
app.controller ("myctrl1", function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}});Inyección de marcador
Este método de inyección requiere establecer una matriz de dependencia, que contiene el nombre del servicio de dependencia. En los parámetros de función, puede establecer el nombre del parámetro a voluntad, pero la consistencia del orden debe garantizarse.
var myctrl2 = function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello (); }} myctrl2. $ injector = ['hello1', 'hello2']; app.controller ("myctrl2", myctrl2);Inyección en línea
Este método de inyección pasa directamente en dos parámetros, uno es el nombre y el otro es una matriz. El último parámetro de esta matriz es el cuerpo del método real, y el resto son todas dependencias, pero es necesario asegurarse de que los parámetros en el cuerpo del método estén en el mismo orden (igual que la inyección de marca).
app.controller ("myctrl3", ['$ scope', 'hello1', 'hello2', function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}}]);Métodos comunes de $ inyector
En angular, el inyector se puede obtener a través de angular.injector ().
var $ injector = angular.injector ();
Obtenga el nombre del servicio de dependencia a través de $ injector.get ('ServiceName')
$ injector.get ('$ alcance')
Obtenga todas las dependencias de xxx por $ injector.annotate ('xxx')
$ inyector.annotate (xxx)
Código de muestra
<html> <fead> <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> </script> <body) ng-concontroller = "myctrl1"> <input type = "Button" ng-cick = "hello ()" value = "ctrl1"> </ input> </div> <div ng ng-concoller = "myctrl2"> <input type = "botón" ng-click = "hello ()" valor = "ctrl2"> </put> </div> <divit <div "button" ng-chick = "hello ()" valor = "ctrl2"> </put> </biv> <divit <div "button" ng-chick = "hello ()" valor = "ctrl2"> </input> </biv> <divit <div "botón" ng-chick = "hello ()" valor = "ctrl2"> </input> </biv> <Div Diver " 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 ();}}); // anotado // función explícita (serviceA) {}; // explícito. $ inject = ['ServiceA']; // $ injector.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 () c.hello ();Lo anterior es una compilación de la información del inyector AngularJS. Continuaremos agregando información relevante en el futuro. ¡Gracias por su apoyo para este sitio web!