Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:18:53+00:00 2026-06-12T15:18:53+00:00

I use Angular $resource for REST service. Due to the quirk in my get

  • 0

I use Angular $resource for REST service. Due to the quirk in my get response, I cannot use $resource service for CRUD application.

Creating a new object work (say for Card), smilar to:

var newCard = new CreditCard();
newCard.name = "Mike Smith";
newCard.$save();

Get also works:

var card = CreditCard().get({_id:1)

But, the GET response is not the object Card itself, but other message with it (wrapper object)

{ message: ".....",
  response: Card //object
}

so when I save the instance retrieved through resource, it sends the wrapper object (with the modified Card object in response field).
This probably correct, but my server expect Card object not the wrapper.
Is there a way to customize $resource so that it sends the desired object. From the doc, looks like only url parameters can be changed.

$resource(url[, paramDefaults][, actions]);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:18:55+00:00Added an answer on June 12, 2026 at 3:18 pm

    I’ve also had problems with the standard implementation in the $resource module. For a while I just made edits in my own local copy of the $resource file, but I found that I still was unhappy with the way they implemented REST resources. I needed more flexibility than was offered.

    The standard $resource module is just a factory wrapper around $http. If you boil down the code in the $resource module you can create your own custom implementation fairly easily.

    var app = angular.module('app', []);
    
    app.factory('CreditCard', ['$http', function($http) {
    
        function CreditCardFactory() {
    
            function parseMessage(message) {
                if (message.response) {
                    return message.response;
                }
            }
    
            function CreditCard(value) {
                angular.copy(value || {}, this);
            }
    
            CreditCard.$get = function(id) {
                var value = this instanceof CreditCard ? this : new CreditCard();
                $http({
                    method: 'GET',
                    url: '/creditcards/' + id
                }).then(function(response) {
                    var data = response.data;
                    if (data) {
                        angular.copy(parseMessage(data), value);
                    }
                });
                return value;
            };
    
            CreditCard.prototype.$get = function(id) {
                CreditCard.$get.call(this, id);
            };
    
            return CreditCard;
    
        }
    
        return CreditCardFactory;
    
    }]);
    

    Then, within your controller function, inject the CreditCard factory just as you would $resource.

    app.controller('CreditCardCtrl', function($scope, CreditCard) {
        $scope.creditCard = CreditCard().get(3);
    });
    

    This allows you to parse the responses of your REST actions anyway you want and also allows you to implement any actions you want. For instance: I wanted a save method on my resources that would check to see if the object had an id property before choosing to use a POST (creating a new resource when no id is available) or a PUT (updating an existing resource when a valid id is available).

    This also would allow you to implement a different way of handling the JSON CSRF Vulnerability. The angular.js way is built into $http, but my company’s REST API fixes this problem by wrapping JSON arrays in a dummy object. I use a custom resource like the one above to parse the dummy object out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to use angular with django i've implemented a REST api with django
I'd like to use socketstream's RPC over websockets abstraction while using Angular for MVC.
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
I have a rest service returning JSON with one number {uptime:44} available under url:
We use Angular mainly for our search form, which is pretty complex. We use
How do I use a custom delimiter for angular JS? I'd like to change
I am using Angular.js for an application that uses hidden trs to simulate a
I'm new to Angular and still learning. I read alot about generating static HTML
I am new to Angular and Flot, but am experienced with Jquery and Javascript.
What is the use of using angular tag in css ie, .class << span

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.