4

I'm trying to create simple "database" in json file, and service for it with function to get specific item from json parsed array. I'm using sample modified code from angular tutorial:

var projectService = angular.module('projectService', ['ngResource']);

projectService.factory('Project', ['$resource', '$http',
function($resource, $http){
    return $resource('assets/data/projects.json', {id: '@id'}, {
        get: {
            method: 'GET',
            transformResponse: function(data) {
                //problem
            }
        }
    });
}]);

I see that correct parameter is passed during ajax call the whole contect of json file is returned, and I cannot access the url param to filter the array to get correct object. Is there any way to access url params in transformResponse function?

Marrbacca
  • 137
  • 1
  • 10
  • The second argument in the $resource provider, paramDefaults, is designed to contain the default URL parameters for parameters states in the first argument, the url. Your URL does not have an :id parameter in it, so in all cases it will just return the whole JSON object. – horyd Oct 19 '14 at 22:10
  • @horyd - yeah, I know that passing parameter to url containing only json file will always return the whole content of the file, and I just want to filter the content of the file manually, but I can't get the url parameters I passed with the request, cause I see that the parameter is correctly passing into request e.g: build/assets/data/projects.json?id=projectId – Marrbacca Oct 20 '14 at 20:22
  • 1
    Ah I see. I looked into the docs on $resource a bit more but I can't find a way to do what you're thinking unfortunately. Sorry :( – horyd Oct 21 '14 at 11:20
  • @horyd - no problem at all, I ended up using $location service, and just parse current url which also had the id i was needed. This isn't ideal solution, but it's enough for me. Thanks for the help :) – Marrbacca Oct 21 '14 at 14:58

0 Answers0