The Difference Between Angular's $http and $resource Services
I tend to stick with the $http service as it works well and has no suprises. But, I can see the allure of the $resource service. Here's a really brief demo.
These two services accomplish the same job: send and retrieve data from an API.
And they both return a promise. So, why would you choose one over the other?
Here’s a quick example of how you might use the
If our API returns an Array of users then we are all set. If we want to do something when there’s an error we’ll need to handle the promise a bit differently.
We could accomplish the same thing like this:
And because this is a promise, it’s “thenable” - meaning you can chain more requests in sequence:
Here’s a quick example of how you might user the
MyResource is defined once and can be used throughout your controller/service to
delete. The nice thing about the
$resource is that it has all of the convenience methods built in. The downside is you have to define your parameters upfront.
Here’s a demo showing both methods.