繁体   English   中英

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

[英]How to remove $promise and $resolved from json object

我在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
    })
}

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

我的项目示例

Diary.getSharedWithMe(function(data) {
        delete data.$promise;
        delete data.$resolved;
        _self.sharedDiariesWithMe = data;
    }, function(error) {
        console.log(error)
    });

这个答案 ,它看起来yourResource.toJSON()随时可用。

是的,我多次遇到同样的问题,总是使用$ http而不是$ resource, 但是 ,今天我决定尝试解决这个问题。 我希望有人也会在某一天找到这个有用的东西。

所以,在你收到带有$ promise的对象之后,你所做的只是使用angular.copy(data) ,如下所示:

someService.getSomething($scope.someId).$promise.then(function (data) {
    if (data) {
        $scope.dataWithoutPromise = angular.copy(data);
    }
});

之后,您将看到dataWithoutPromise只包含您需要的对象,$ promise和$ resolved消失了。

我面临同样的问题。 实际上,您只需使用相同版本的角度和角度资源,它就像魅力一样。 希望能帮助到你 !

简答:angular.fromJson(angular.toJson(resp)); 这将删除所有“角度特定”功能等,并将返回一个干净的数据对象。

您是否在您的$resource对象中使用isArray.query()

来自$ resource docs: isArray – {boolean=} – If true then the returned object for this action is an array.

更新:

如果您正确使用$resource ,您可以使用以下选项:

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

2)如果无法做到这一点,请使用$resource.query()的回调,例如。

MyResource.query({ myParams: 'something'}, function(result) {
    // Do something with the plain result
});

我为angular.toJson做了这个快速可重用的方法(带有前导$$的属性将被剥离):

yourApp.factory('Service', ['$resource', function ($resource) {
    return $resource('your/rest/api/path');
}]);

 yourApp.factory('commonServices', function () { 
         return {
                toJson : function(response){
                    return JSON.parse(angular.toJson(response));
                     //*or* 
                    //return angular.toJson(response);
                }
    });

然后在你的控制器中这样的事情:

 yourApp.controller('Controller', ['$scope', 'Service','commonServices',  
function ($scope, Service, commonServices) {
            Service.get().then(function (data) {
                $scope.myData = commonServices.toJson(data);
            });
        });

更简单的方法是在控制器中添加angular.toJson

 yourApp.controller('Controller', ['$scope', 'Service',  
function ($scope, Service, commonServices) {
            Service.get().then(function (data) {
                $scope.myData = angular.toJson(data);
            });
        });

请简单地这样做:

jsonObject。$ promise = undefined; jsonObject。$ resolved = undefined;

$ promise和$ resolved不会被删除,但你可以做你想要的。

尝试在您的服务中编写类似的代码:

yourApp.factory('Service', ['$resource', function ($resource) {
    return $resource('your/rest/api/path');
}]);

然后在你的控制器中这样的事情:

yourApp.controller('Controller', ['$scope', 'Service', function ($scope, Service) {
    Service.get().then(function (data) {
        $scope.myData = data;
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM