編輯:這個問題已棄用。請參閱How to set a variable from an $http call then use it in the rest of the application WITHOUT making the whole application asynchronous。
我有一個factory
但$http
是未定義的。我以爲DI會爲我定義它。
我在做什麼錯?我在這裏犯了什麼根本的錯誤理解?
額外的零件()
導致函數在配置階段被調用。但URL
是我希望馬上爲整個應用程序提供的屬性。
angular.module('myApp').factory('Test',['$http'],
{
URL: (function ($http) {
$http.get('http:www.myserver.com/api/thing').then(function (response) { });
})() // extra parens makes the function run right off the bat
}
);
我該如何解決?
'//額外的括號的功能,使功能運行正確關閉bat'它使其將URL屬性設置爲undefined ... –
提示:當您使用該數組語法指定依賴項時,數組中的最後一項需要是一個函數:'angular.module('myApp')。factory ('Test',['$ http',function($ http){...});' –