且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何遍历$资源返回的查询并获得所需的值?

更新时间:2023-11-07 12:19:52

您的呼叫后,立即检查查询的结果,但ngResource是异步的,所以也许数据尚未从服务器的时候返回你试图访问它。尝试把你的访问传递给查询回调函数()。

You are inspecting the results of the query immediately after the call, but ngResource is asynchronous, so perhaps the data has not yet returned from the server by the time you are trying to access it. Try putting your access in the callback function passed to query().

  $scope.cars = Cars.query(function() {
      console.log($scope.cars);
      console.log($scope.cars[0]._id);
  });