如何从json对象中删除$ promise和$ resolved

use*_*100 17 javascript angularjs

我在angular中使用$ resource来获取json对象,其结构定义如下

[
    {
        "id": 0,
        "name": "Type1"
    },
    {
        "id": 1,
        "name": "Type 2"
    }
]
Run Code Online (Sandbox Code Playgroud)

获取数据后... console.log(jsonObject)给了我

[Resource, Resource, $promise: Object, $resolved: true]
Run Code Online (Sandbox Code Playgroud)

如何从结果对象中删除$ promise和$ resolved?我尝试了angular.fromJson(json),但我仍然看到这些对象仍然存在.

Ser*_*lov 8

我正在寻找相同的答案,但此刻我只知道这个丑陋的黑客:

写一个像这样的函数:

function cleanResponse(resp) {
    return JSON.parse(angular.toJson(resp));
}
Run Code Online (Sandbox Code Playgroud)

并在每个查询中调用它(丑陋,是的):

function getSmt() {
    Resource.get({}, function (data) {
        $scope.some = cleanResponse(data);
    }, function (responce) {
        //handle error
    })
}
Run Code Online (Sandbox Code Playgroud)

如果有人教我如何正确地做,我会很高兴


Mat*_*Way 0

您正在使用isArray.query()与您的$resource物体一起使用吗?

\n\n

来自 $resource 文档:isArray \xe2\x80\x93 {boolean=} \xe2\x80\x93 If true then the returned object for this action is an array.

\n\n

更新:

\n\n

如果您使用$resource正确,您有一些选项:

\n\n

1)让后端返回对象中包含的数据,例如。{ data: [] }而不仅仅是一个数组。

\n\n

2) 如果不可能,请使用来自 的回调$resource.query(),例如。

\n\n
MyResource.query({ myParams: 'something'}, function(result) {\n    // Do something with the plain result\n});\n
Run Code Online (Sandbox Code Playgroud)\n