AngularJSのドキュメントで、インジェクターについてこれを見ました:
// You write functions such as this one.
function doSomething(serviceA, serviceB) {
// do something here.
}
// Angular provides the injector for your application
var $injector = ...;
///////////////////////////////////////////////
// the old-school way of getting dependencies.
var serviceA = $injector.get('serviceA');
var serviceB = $injector.get('serviceB');
// now call the function
doSomething(serviceA, serviceB);
///////////////////////////////////////////////
// the cool way of getting dependencies.
// the $injector will supply the arguments to the function automatically
$injector.invoke(doSomething); // This is how the framework calls your functions
良い感じ。しかし、私はそれを取得しません。インジェクターが依存関係を探す最後の行では、グローバル変数 serviceA、serviceB を持っているのとまったく同じではありませんか? つまり、次のように書き直すとします。
var serviceA, serviceB;
function doSomething() {
// access serviceA, serviceB
}
インジェクターにそれをさせる利点は何ですか?つまり、彼が引数に適したオブジェクトを魔法のように見つけることができれば、それらがグローバル変数である場合と同じように簡単に見つけることができるということではないでしょうか?
私の質問が明確であることを願っています...