想不通为什么NG-资源将不会返回数组 [英] Can't figure out why ng-resource won't return array

查看:280
本文介绍了想不通为什么NG-资源将不会返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS和NGResource,我想不通为什么当我使用的查询,我不断收到一个空数组回来了。

在我的控制器,我做

 任务= $资源('/任务');
VAR任务= Task.query(函数(){});
的console.log(任务);
$ scope.tasks =任务;

在视图

  {{任务}}

在认为它正确显示

<$p$p><$c$c>[{\"created_at\":\"08/08/2013\",\"created_by_id\":2,\"description\":\"description\",\"id\":1,\"name\":\"test任务1,parent_task_id:空,task_type_id:1,的updated_at:2013年8月8日},
{created_at:2013年8月8日,created_by_id:2,说明:说明,ID:2,名:测试任务2,task_type_id:1,的updated_at:2013年8月8日}]

但在控制台中它被记录为空数组:

  []

另外,我使用的镀铬batarang延伸,因为它显示出任务具有范围是:

 测试:
  []

我需要在返回值进行一些数据操作我把它改成了$范围模型前。这是典型行为和孤单我丢失的东西?还是我做错了什么?任何想法将AP preciative。我一直停留在这个太久。


解决方案

您正在登录空阵列之前,它已经通过异步AJAX结果填充。

的角的法宝之一是,它会自动更新任务数组新的数据,因为它归结线,同时也自动更新视图。

为了证明,试试这个:

  VAR任务= $资源('/任务');$ scope.tasks = Task.query(函数(){});;$范围。$腕表('任务',功能(价值){
    的console.log(值);
});

下面是从的文档


  

要认识到,调用$资源对象方法立即返回一个空引用(对象或数组取决于IsArray的)是很重要的。 一旦数据从现有的参考填充与实际数据服务器返回。这是一个有用的技巧,因为通常是资源被分配到模型,然后通过视图呈现。具有在没有呈现一个空对象的结果,一旦数据从服务器到达则该对象用该数据填充和视图自动重新呈现本身示出了新的数据。这意味着,在大多数情况下,一个从来没有写一个回调函数的操作方法。


I am using AngularJS and NGResource and I can't figure out why when I use query, I keep getting an empty array back.

In my controller, I am doing

Task = $resource('/tasks'); 
var tasks = Task.query(function () {});
console.log(tasks);
$scope.tasks = tasks;

In the view

{{tasks}}

In the view it displays correctly

[{"created_at":"08/08/2013","created_by_id":2,"description":"description","id":1,"name":"test task 1","parent_task_id":null,"task_type_id":1,"updated_at":"08/08/2013"},
{"created_at":"08/08/2013","created_by_id":2,"description":"description","id":2,"name":"test task 2","task_type_id":1,"updated_at":"08/08/2013"}]

but in the console it gets logged as an empty array:

[]

Also, i'm using batarang extension for chrome and the scope that it shows tasks as having is:

tests: 
  [  ]

I need to perform some data operations on the returned value before I put it into the $scope model. Is this typical behavior and theres something i'm missing? Or am I doing something wrong? Any ideas would be appreciative. I've been stuck on this for too long.

解决方案

You're logging the empty array before it's been populated by the asynchronous AJAX result.

Part of Angular's magic is that it'll automatically update the tasks array with the new data as it comes down the wire, and also auto update the view.

As proof, try this:

var Task = $resource('/tasks');

$scope.tasks = Task.query(function () {});;

$scope.$watch('tasks', function (value) {
    console.log(value);
});

Here's a quote from the docs:

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most case one never has to write a callback function for the action methods.

这篇关于想不通为什么NG-资源将不会返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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