我正在尝试使用$ resource我有一个服务帖子
app.factory('Post', function ($resource, $cookieStore) {
return $resource('http://localhost:port/posts/:id', {"id":"@id", port:':9000'}, {
query: { method:'GET', isArray: true , headers: {'X-AUTH-TOKEN':'authToken='+$cookieStore.get("authToken")}},
save: { method:'POST',headers: {'X-AUTH-TOKEN':'authToken='+$cookieStore.get("authToken")}},
update: {method:'PUT' ,headers: {'X-AUTH-TOKEN':'authToken='+$cookieStore.get("authToken")}},
delete : {method: 'DELETE',headers: {'X-AUTH-TOKEN':'authToken='+$cookieStore.get("authToken")}},
get : { method: 'GET', headers: {'X-AUTH-TOKEN':'authToken='+$cookieStore.get("authToken")}}
});
});
在控制器中我试图获得所有帖子。
Post.query(function(data){
console.log(data.length);
console.log(data)
});
我原本期望数据是从服务器返回的,但是console.log向我展示了如何从服务器访问实际的json数据?
3 posts.js:18
[Resource, Resource, Resource, $promise: Object, $resolved: true]
0: Resource
1: Resource
2: Resource
$promise: Object
$resolved: true
length: 3
__proto__: Array[0]
答案 0 :(得分:1)
根据documentation,行动中的第一个参数似乎是查询的参数对象,然后才成功回调。所以试试:
Post.query({}, function(data) {
console.log(data.length);
console.log(data);
})
你也可以使用$ promise:
Post.query({}).$promise.then((function(data) {
console.log(data.length);
console.log(data);
})