0

How do i get the get JSON data out of a $resource?

Currently all return values out of User.query and User.$save() are factory objects. I would like to be able to keep $scope.users as raw JSON so I can modify it and post the data to another URL.

 myApp.factory("User", function($resource) {
    return $resource("/api/users/:Id", { Id: "@Id"}, {
      "update": {method:"PUT"},
      "query": { method:"GET", isArray: true }
    });
});

var users = User.query(function() {
    $scope.users = users;
});
mphuie
  • 970
  • 1
  • 9
  • 17

1 Answers1

0

You can transform the response like this:

myApp.factory("User", function($resource, $http) {
    return $resource("/api/users/:Id", { Id: "@Id"}, {
        "update": {method:"PUT"},
        "query": {
            method:"GET",
            isArray: true,
                transformResponse: [function (data, headersGetter) {
                return [{ result: JSON.parse(data) }];
            }].concat($http.defaults.transformResponse)
        }
    });
});
francisco.preller
  • 6,559
  • 4
  • 28
  • 39