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

2023-09-13 05:01:16 作者:Gorgeous 绚丽

我使用MEANJS

在我的控制器,我有

//找车名单    $ scope.findHome =功能(){      $ scope.cars = Cars.query();      的console.log($ scope.cars);    };

它输出

在这里,我想第一个数组中的_id字符串 0:资源

here i want to get the _id string inside the first array 0: Resource

我试过 $ scope.cars [0] ._ ID 返回不确定的,请帮帮忙。

I tried $scope.cars[0]._id which returns undefined, Please help.

推荐答案

您的呼叫后,立即检查查询的结果,但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);
  });