5

I'm a beginner with AngularJS, and can't find my error in this code

   var myCarResource = $resource('cars/:carId', 
        {
            carId:'@id'
        });
    var car = myCarResource.get({id:'abc'});

Expected URL : .../cars/abc

The called URL is: .../cars?id=abc

I'm using angularjs v1.2.24

Can anyone help me? Thanks

1

1 Answer 1

5

As stated in the $resource paramDefaults documentation:

Given a template /path/:verb and parameter {verb:'greet', salutation:'Hello'} results in URL /path/greet?salutation=Hello.

If the parameter value is prefixed with @ then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling an action method). For example, if the defaultParam object is {someParam: '@someProp'} then the value of someParam will be data.someProp

This suggests that any verb defined in the parameterizd url that matches the keys defined in the $resource's parameter defaults or the $resource class methods(get, save, etc..) parameters will have the corresponding value of that key replace the verb in the url. The '@' notation on the other hand, was not explained properly in this context, it should have been:

If the parameter value is prefixed with @ then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling an instance action method).

Instance action methods($get, $save, $delete, etc..), are methods that are used for data objects retrieved using $resource class action methods. These are usually helpful when chaining requests with the same resources.

EXAMPLE DEMO

Let's assume that your cars/abc returns a response json of:

{
  "id": "abc"
}

Read the comments showing the responses of each action method invocation.

var myCarResource = $resource('cars/:carId', 
{
  carId:'@id'
});


// This sends a GET request '/cars/?id=abc
myCarResource.get({id:'abc'}); 

// This sends a GET request '/cars/abc'
myCarResource.get({carId:'abc'}); // returns {"id": "abc"}

myCarResource.get({carId:'abc'}).$promise.then(function(car) {

  // sends a POST request '/cars/abc', it replaces the :carId verb from the
  // @id notation you have defined in the parameter default. It also sends,
  // other parameter defaults defined with '@' that are defined as verbs in the url.
  car.$save();

}); 
Sign up to request clarification or add additional context in comments.

1 Comment

From @Yalamber comment/link above, another option is to do the following: myCarResource.get({},{id:'abc'}); From my limited understanding, I believe that the first object is directly related to the URL pattern, and thus must match the names of the :param in the URL (in this case, carId). The second object is the one that is used when you use '@id' and will pull the second object's ID and match it to :carId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.