Thesimplestwaytogetholdofthedependencies,istoassumethatthefunctionparameternamesarethenamesofthedependencies.functionMyController($scope,greeter){...}Givenafunctiontheinjectorcaninferthenamesoftheservicetoinjectbyexaminingthefunctiondeclarationandextractingtheparameternames.Intheaboveexample$scope,andgreeteraretwoserviceswhichneedtobeinjectedintothefunction.上文是在DependencyInjection這篇Guide中截去的原文。其中講到如果Controller需要某樣服務,則只需在他構造函數的參數里添加并指定正確的名稱就行了。像上面的例子里,Angular會自動尋找$scope和greeter這兩個服務,并傳遞給函數。但是令我不解的是,Angular是如何知道MyController簽名(參數)的?和Function參數相關的變量似乎只有arguments,但這個變量只能在函數內部使用,在外部調用會返回null。functionACtrl(paramA,paramB){}ACtrl.arguments//null那么,Angular到底是如何知道函數簽名(參數)的?
Angular.js Dependency Injection 的實現原理是什么?
HUX布斯
2019-04-07 09:38:35