我如何删除请求对象中的文件在angularjs

htzpubme  于 2023-03-22  发布在  Angular
关注(0)|答案(3)|浏览(122)

我从angularjs控制器向服务器请求获取一个对象,我想在视图显示对象之前删除对象中的一个字段。

$scope.findOne = function() {
    $scope.order = Orders.get({
        orderId: $stateParams.orderId
    });
    delete $scope.order._id;
    console.log(JSON.stringify($scope.order));
};

它打印

{"$promise":{},"$resolved":false}

它不会删除id字段。我的视图显示我的对象的even _id。我如何删除该字段?
以下是我的服务文件
“使用严格”;

//Orders service used for communicating with the orders REST endpoints
angular.module('orders').factory('Orders', ['$resource',
    function($resource) {
        return $resource('orders/:orderId', {
            orderId: '@_id'
        }, {
            update: {
                method: 'PUT'
            }
        });
    }
]);

当我将findOne()更改为

$scope.findOne = function() {
    Orders.get({
        orderId: $stateParams.orderId
        }).success(function(res)//(this is line number 56)
        {
            delete res.data._id;
            $scope.order = res.data;
    });
};

我在控制台中得到此错误

TypeError: undefined is not a function
    at Scope.$scope.findOne (http://localhost:3000/modules/orders/controllers/orders.client.controller.js:56:14)
    at http://localhost:3000/lib/angular/angular.js:10903:21
    at Scope.$eval (http://localhost:3000/lib/angular/angular.js:12811:28)
    at pre (http://localhost:3000/lib/angular/angular.js:20125:15)
    at nodeLinkFn (http://localhost:3000/lib/angular/angular.js:6732:13)
    at compositeLinkFn (http://localhost:3000/lib/angular/angular.js:6146:13)
    at publicLinkFn (http://localhost:3000/lib/angular/angular.js:6042:30)
    at http://localhost:3000/lib/angular-ui-router/release/angular-ui-router.js:3905:9
    at nodeLinkFn (http://localhost:3000/lib/angular/angular.js:6752:13)
    at compositeLinkFn (http://localhost:3000/lib/angular/angular.js:6146:13) <section data-ng-controller="OrdersController" data-ng-init="findOne()" class="ng-scope">angular.js:10126 (anonymous function)
tvokkenx

tvokkenx1#

get默认返回一个promise对象。方法是

$scope.findOne = function() {
    Orders.get({
        orderId: $stateParams.orderId
        }).success(function(res)
        {
            delete res.data._id;
            $scope.order = res.data;
    });
};
yshpjwxd

yshpjwxd2#

我把函数改成了

$scope.findOne = function() {
        var order = Orders.get({
            orderId: $stateParams.orderId
        }, function()
        {
            order.order_type = 'new';
        });
         $scope.order = order;
    };

成功了。我想知道语法是否在angularjs更新中改变了。类似的语法在https://docs.angularjs.org/api/ngResource/service/ $resource中提到过。虽然我不知道其他成功回调的答案不起作用的原因。

b09cbbtk

b09cbbtk3#

Orders.get函数返回一个promise,记住这一点,你的代码应该看起来像这样:

Orders.get({orderId: $stateParams.orderId}).then(function(order) {
  delete order._id;
  $scope.order = order;
});

相关问题