如何遍历$资源返回的查询并获得所需的值? [英] How to loop through $resource returned query and get desired value?

查看:124
本文介绍了如何遍历$资源返回的查询并获得所需的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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);
  });

这篇关于如何遍历$资源返回的查询并获得所需的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆