數組是否必需?AngularJs依賴注入差異
app.controller('myController', ['$scope', function($scope){
}])
這個工作是否像上面的代碼?
app.controller('myController', function($scope){
})
數組是否必需?AngularJs依賴注入差異
app.controller('myController', ['$scope', function($scope){
}])
這個工作是否像上面的代碼?
app.controller('myController', function($scope){
})
該數組始終是必需的,但它不必組合到控制器定義中。有一個更乾淨的方式來做到這一點,它是我的首選方法。我所做的是以功能性的方式創建控制器。例如:
app.controller('myController', MyController);
//dependency injection done here using the array of definitions
MyController.$inject = ['$scope'];
//all of the dependencies are added as parameters into the controller function
function MyController ($scope) {
//insert controller code
}
正如Sajeetharan在答案中指出的那樣,這被稱爲顯式方法。您應該查看獲得Angular 1團隊認可的this style guide其他模式,這些模式有助於使代碼更清潔,更易於維護
是的,它會以同樣的方式工作,它就是difference.You需要使用explicit
依賴注入(第二種方法)。
即使縮小它將$ scope轉換爲變量a並將$ http轉換爲變量b,它們的標識仍保留在字符串中。
謝謝!這只是一個簡單的問題,因爲我從來沒有使用顯式依賴注入 –