Mistalis Mistalis -4 years ago 152
AngularJS Question

Pass in parameters with Angular resource service

I want to send some parameters to perform a GET on my server, and one must be in my URL (the

collabId
).

Here is my service:

/* Service */
angular.module('app').factory('CollabService', function($resource) {
return $resource('/collabs/:collabId', {collabId:'@collabId'}, {
getCollabs: {
method: 'GET',
headers: {'Content-Type': 'application/json'},
isArray: true
}
});
});


Here is my controller. I want to send the parameters in
$scope.q
, and have my
collabId
in the URL.

I've tried this, but it does not work (
400 Bad Request
):

/* Controller */
$scope.q = {
"collabId": 30;
"firstWeek": 1;
"nbWeeks": 4;
"year": 2015
};
CollabService.getCollabs($scope.q, {collabId: $scope.q.collabId},
function(col) { //Print result
console.log(col);
});


I think I miss something in the syntax of my query in my controller. Can you help me finding what is wrong here ?

Thanks :)

Answer Source

There is an error in the CollabService. Here you pass the whole $scope.q object:

CollabService.getCollabs($scope.q, {collabId: $scope.q.collabId},
    function(col) { //Print result
       console.log(col);
});

It should be changed to:

CollabService.getCollabs({collabId: $scope.q.collabId}, // <--
    function(col) { //Print result
       console.log(col);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download