use*_*100 17 javascript angularjs
我在angular中使用$ resource来获取json对象,其结构定义如下
[
    {
        "id": 0,
        "name": "Type1"
    },
    {
        "id": 1,
        "name": "Type 2"
    }
]
获取数据后... console.log(jsonObject)给了我
[Resource, Resource, $promise: Object, $resolved: true]
如何从结果对象中删除$ promise和$ resolved?我尝试了angular.fromJson(json),但我仍然看到这些对象仍然存在.
我正在寻找相同的答案,但此刻我只知道这个丑陋的黑客:
写一个像这样的函数:
function cleanResponse(resp) {
    return JSON.parse(angular.toJson(resp));
}
并在每个查询中调用它(丑陋,是的):
function getSmt() {
    Resource.get({}, function (data) {
        $scope.some = cleanResponse(data);
    }, function (responce) {
        //handle error
    })
}
如果有人教我如何正确地做,我会很高兴
您正在使用isArray或.query()与您的$resource物体一起使用吗?
来自 $resource 文档:isArray \xe2\x80\x93 {boolean=} \xe2\x80\x93 If true then the returned object for this action is an array.
更新:
\n\n如果您使用$resource正确,您有一些选项:
1)让后端返回对象中包含的数据,例如。{ data: [] }而不仅仅是一个数组。
2) 如果不可能,请使用来自 的回调$resource.query(),例如。
MyResource.query({ myParams: 'something'}, function(result) {\n    // Do something with the plain result\n});\n| 归档时间: | 
 | 
| 查看次数: | 11746 次 | 
| 最近记录: |