gpt4 book ai didi

javascript - 如何删除 angularjs 中请求对象中的字段

转载 作者:行者123 更新时间:2023-11-28 19:27:11 25 4
gpt4 key购买 nike

我从 angularjs Controller 向服务器发出请求以获取一个对象,并且我想在 View 显示该对象之前删除该对象中的一个字段。以下是我的代码

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

打印

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

它不会删除 id 字段。我的 View 甚至显示了我的对象的 _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)

最佳答案

get 默认返回一个 Promise 对象。这样做的方法是

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

关于javascript - 如何删除 angularjs 中请求对象中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27634826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com