javascript AngularJS $resource 查询返回带有函数的数组,这在迭代数据时不太合适
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22968274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS $resource query returns array with a function in it, which is not does not fit well when iterating through data
提问by Xavitoj Cheema
I have added a custom Array.prototype.functionName to my js file. I am using AngularJS 1.2.3 and when I call $resource to return a query, it appends that custom function in the response array. when i iterate over the response array, it return exceptions when parsing the custom function, that is what i believe. Can anyone suggest me how to work around this problem
我在我的 js 文件中添加了一个自定义的 Array.prototype.functionName。我使用的是 AngularJS 1.2.3,当我调用 $resource 返回查询时,它会在响应数组中附加该自定义函数。当我遍历响应数组时,它在解析自定义函数时返回异常,这就是我所相信的。谁能建议我如何解决这个问题
Array.prototype.functionName = function(){
//sorting
}
I call my $resource like this in my service
我在我的服务中这样称呼我的 $resource
return $resource('/url',{},{
'List':{
method: 'GET',
isArray: true
}
});
and call it in my controller
并在我的控制器中调用它
returnResource.List().$promise.then(function(Data){
console.log(Data); //Data has my custom function append at the end
for(var i in Data){
//do something
}
});
回答by Nix
You are looping over the properties([0],[1], length, $promise, etc)
of the array versus the items.
您正在遍历properties([0],[1], length, $promise, etc)
数组与项目的 。
For in
is not correct in this case that says hey I want to loop over every property of this array which includes the array items, but it also includes some other ng-resource`y things.
Forin
在这种情况下是不正确的,它说嘿我想循环这个数组的每个属性,其中包括数组项,但它还包括一些其他的 ng-resource`y 东西。
You should be using angular.forEach
, Data.forEach
, or go old school and use a for(i;i<Data.length;i++)
你应该使用angular.forEach
, Data.forEach
, 或者去老派使用for(i;i<Data.length;i++)
I can see you don't fully understand what ng-resource
is returning; The response from the query/array looks like this:
我可以看到你并不完全理解ng-resource
返回的是什么;来自查询/数组的响应如下所示:
sort me![Resource, Resource, $promise: Object, $resolved: true, ...]
0: Resource
id: "nix"
__proto__: Resource
1: Resource
id: "nix2"
__proto__: Resource
$promise: Object
$resolved: true
length: 2
__proto__: Array[0]
.. array funcs
forEach: function forEach() {
[native code]
}
functionName: function () {
}
...
Array.prototype.functionName = function(){
//sorting
console.log("what is this...?", this, this.length);
this.forEach(function(item){
console.log(item);
});
}
app.controller('TestCtrl', function($scope, $resource) {
$scope.resource = $resource('url.json',{},{
'List':{
method: 'GET',
isArray: true
}
});
$scope.resource.List().$promise.then(
function(data){
console.log("List", this);
data.functionName();
});
$scope.resource.query().$promise.then(
function(data){
console.log("query", this);
data.functionName();
});
});
I included the out of the box way to do REST List
using the query. Your List
call and my Query
call are doing the exact same thing they are hitting the url /Api
and are expecting an array to be returned.
我包含了List
使用查询执行 REST 的开箱即用方式。您的List
电话和我的Query
电话正在执行完全相同的操作,他们正在访问 url/Api
并期望返回一个数组。