How to loop through $resource returned query and get desired value?

↘锁芯ラ 提交于 2019-12-23 20:34:14

问题


I am using MEANJS

In my controller i have

// Find a list of Cars $scope.findHome = function() { $scope.cars = Cars.query(); console.log($scope.cars); };

Which outputs

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

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


回答1:


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);
  });


来源:https://stackoverflow.com/questions/28546568/how-to-loop-through-resource-returned-query-and-get-desired-value

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