推測された注射
この注入方法では、パラメーター名がサービス名と同じであることを確認する必要があります。コードを圧縮する必要がある場合など、注入は失敗します。
app.Controller( "myctrl1"、function($ scope、hello1、hello2){$ scope.hello = function(){hello1.hello(); hello2.hello();}});マーカー噴射
この注入方法には、依存関係サービス名を含む依存関係配列を設定する必要があります。関数パラメーターでは、パラメーター名を自由に設定できますが、順序の一貫性を確保する必要があります。
var myctrl2 = function($ scope、hello1、hello2){$ scope.hello = function(){hello1.hello(); hello2.hello(); }} myctrl2。$ injector = ['hello1'、 'hello2']; app.controller( "myctrl2"、myctrl2);インラインインジェクション
この注入方法は2つのパラメーターに直接渡されます。1つは名前で、もう1つは配列です。この配列の最後のパラメーターは実際のメソッド本体であり、残りはすべて依存関係ですが、メソッド本体のパラメーターが同じ順序であることを確認する必要があります(マークインジェクションと同じ)。
app.Controller( "myctrl3"、['$ scope'、 'hello1'、 'hello2'、function($ scope、hello1、hello2){$ scope.hello = function(){hello1.hello(); hello2.hello();}}}}]);$インジェクターの一般的な方法
Angularでは、インジェクターはAngular.injector()を介して取得できます。
var $ injector = angular.injector();
$ injector.get( 'servicename')を介して依存関係の名前を取得します
$ injector.get( '$ scope')
$ injector.annotate( 'xxx')でxxxのすべての依存関係を取得します
$ injector.annotate(xxx)
サンプルコード
<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.min.min.js ng-controller = "myctrl1"> <input type = "button" ng-click = "hello()" value = "ctrl1"> </input> </div> <div ng-controller = "myctrl2"> <入力タイプ= "button" ng-click = "hello()" value = " <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($ injector){return $ injector;})、$ injector)); app.Controller( "myctrl1"、function($ scope、hello1、hello2){$ scope.hello = function(){hello1.hello(); hello2.hello();}}); // ANNOTATED //関数explicit(servicea){}; //明示的。$ inject = ['servicea']; // $ injector.invoke(expricit); 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、c){// a.hello(//) c.hello();上記は、AngularJSインジェクターの情報を編集しています。今後も関連情報を追加し続けます。このウェブサイトへのご支援ありがとうございます!