Issue
I make request to server from angularjs controller to get an object and I want to delete a field in the object before view displays it. Folloiwng is my code
$scope.findOne = function() {
$scope.order = Orders.get({
orderId: $stateParams.orderId
});
delete $scope.order._id;
console.log(JSON.stringify($scope.order));
};
it prints
{"$promise":{},"$resolved":false}
it doesn't delete the id field. My view shows even _id of my object. How do I delete that field?
Following is my services file
'use strict';
//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'
}
});
}
]);
When I change findOne() to
$scope.findOne = function() {
Orders.get({
orderId: $stateParams.orderId
}).success(function(res)//(this is line number 56)
{
delete res.data._id;
$scope.order = res.data;
});
};
I get this error in console
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)
Solution
I changed the function to
$scope.findOne = function() {
var order = Orders.get({
orderId: $stateParams.orderId
}, function()
{
order.order_type = 'new';
});
$scope.order = order;
};
and it worked. I wonder if the syntax is changed in angularjs update. Similar syntax is mentioned in https://docs.angularjs.org/api/ngResource/service/$resource. Though I could not figure out the reason other answers with success callback didn't work .
Answered By - raju
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.