![](/img/trans.png)
[英]Provider 'xx' must return a value from $get factory method in AngularJs
[英]Provider 'xx' must return a value from $get factory method
這是我的角度服務:
angular
.module('myApp')
.factory('partyService', function($http) {
this.fetch = function() {
return $http.get('/api/parties')
.then(function(response) {
return response.data;
});
}
});
我認為這個服務正在返回數據(或者一個空數組//不確定)
那我為什么要糾結這個錯誤:
錯誤:[$ injector:undef]提供者'partyService'必須從$ get factory方法返回一個值。
更新:
如果我使用服務而不是工廠,那么我不會有任何錯誤。 為什么這樣???
這個錯誤不言而喻。 你必須在工廠歸還一些東西:
var factory = {
fetch: fetch
};
return factory;
function fetch() {
return..
}
然后,在你的controller
:
partyService.fetch()...
angular
.module('myApp')
.factory('partyService', function($http) {
function fetch = function() {
return $http.get('/api/parties')
.then(function(response) {
return response.data;
});
}
function anotherFetch = function() {
return $http.get('/api/parties')
.then(function(response) {
return response.data;
});
}
return {
fetch,
anotherFetch,
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.