亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular.js Dependency Injection 的實現原理是什么?

Angular.js Dependency Injection 的實現原理是什么?

HUX布斯 2019-04-07 09:38:35
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到底是如何知道函數簽名(參數)的?
查看完整描述

2 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

AngularJS是通過靜態編譯來分析參數的。
functionACtrl(paramA,paramB){}
ACtrl.toString()
"functionACtrl(paramA,paramB){}"
通過對函數toString()操作,獲取到函數全部內容,然后進行編譯處理。
壞處是不抗壓縮。
                            
查看完整回答
反對 回復 2019-04-07
  • 2 回答
  • 0 關注
  • 278 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號