I'm trying angular.js for the first time. I have my rest service configured the following way:
get('/api/users') //returns a JSON Array with all the users
get('/api/users/:id') //returns a JSON object with the requested ID
My angular Controller is set up like this:
UserCtrl.factory('User', function($resource) {
return $resource('/api/users/:id', { id: '@id' }, { update: { method: 'PUT' } });
});
var EditCtrl = function($scope, $location, $routeParams, User){
var id = $routeParams._id;
$scope.user = User.get({id: id});
};
My problem is that when i run
User.get({id: id})
the URL requested is:
http://localhost:8080/api/users?id=389498473294
I want it to be
http://localhost:8080/api/users/389498473294
I can do it using $http, but i think .get() should be able to do it...
thanks
You define the default value for the id parameter with a prefixed @, that means that the value must be taken from the object passed as parameter to the call. In a get call there is no object sent, so the id parameter is assigned the null value. Being the id parameter already assigned, the value you pass is appended as a query parameter. For an explanation of this look at angular documentation under the Usage/Param defaults paragraph. The correct way to declare the service should be:
UserCtrl.factory('User', function($resource) { return $resource('/api/users/:id', { id: '' }, { update: { method: 'PUT' } }); });