ngResource makes a request but does not parse results

江枫思渺然 提交于 2019-12-12 06:14:13

问题


I am working on a webapp that uses ngResource to fetch backend data. Here is my factory

App.factory('MyCoolResource', ['$resource', function($resource) {
  'use strict';
  return $resource('/rest/MyCoolResource');
}]);

Then in a Controller

console.log("Query: " + MyCoolResource.query())

In Chrome network inspector I can see the data coming back (an array of String)

["Foo","Bar","Gaz","Waka"]

But the console logging shows nothing:

Query:    

What am I doing wrong here?


回答1:


The console.log() is getting called before the data arrives, because MyCoolResource.query() is asynchrone, try to use a callback that will be executed once the query ended and the data returned from the API then show this data via the console.log():

MyCoolResource.query(function(result) {
    console.log(result);
});


来源:https://stackoverflow.com/questions/28932589/ngresource-makes-a-request-but-does-not-parse-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!