我想在調用$ http的get方法之前添加一些頭文件。我嘗試這樣做:做些什麼並繼續執行類執行
$http = $originalHttpMethod;
this.$http = function() {
//add headers
return $http
};
$http.get({url:'http://...'});
,但我得到這個錯誤:
TypeError: Object function() {return $http} has no method 'get'.
我想這樣做沒有崩潰原始類。
我該如何做到這一點?
在你的榜樣,服務名稱將是'控制器,而不是'$ myHttp'內myHttp'。 – btford
對。固定。謝謝你選擇了。 – matsko
如果你的服務函數返回一個對象,最好使用factory()而不是service()。 service()是用於構造函數的。如果感興趣,請參閱http://stackoverflow.com/questions/13395686/angularjs-service-in-separate-file/13406613#13406613獲取更多信息。 –