Yeah! I know what you are thinking.
Yet another article on Angular
$resource!?! If someone could just go ahead and kill me, that’d be great.
Well, just hang in there. This will be worth your time, trust me.
Not only the developers who are newly exposed to AngularJS but I have also seen many experienced ones’s struggling with the differences between
$reource. And that’s also the reason why it is so popular interview question for AngularJS.
AngularJS docs define
$http as follows:
The $http service is a core AngularJS service that facilitates communication with the remote HTTP servers via the browser’s XMLHttpRequest object or via JSONP.
Angular JS docs define
$resource as follows:
A factory which creates a resource object that lets you interact with RESTful server-side data sources.
In simple terms
$http lets you make asynchronous calls to communicate with your web service.
$resource lets you make asynchronous calls to communicate with your RESTFUL web service.
So, one could say, they both do exactly the same thing. The only difference is the use-cases they are used in (the “RESTFUL” part).
Like me, many people coming from jQuery background can relate to this service pretty quickly. That’s one of the reasons why it is recommended for beginners to start with
$http when learning AngularJS.
For jQuery junkies- you can think
So, that means, whenever you want to make a general purpose AJAX call, use
$http service by AngularJS. Let’s say there is a
.json file on the server and you want to get data from it. Using
$http service you could do something like this:
But, the use case for
$http doesn’t just stop there. You can also use
$http for communicating with RESTful services.
Recently, RESTful API’s have become very popular. So, if you are dealing with such web service which is RESTful- go with
Simple GET call using
$resource can be done like this-
$resource service is specially designed for dealing with RESTful APIs which makes your code look beautiful when making CRUD operations and transforming the requests and response data.
So let’s see the similarities and differences between
Both are used to make asynchronous calls to the web service.
Both allow the CRUD operations.
Both return promises.
Both can transform requests and responses.
Both allow you to update headers.
$http is used to make general purpose AJAX calls.
$resource is used to communicate with RESTful API.
$resource is actually a subset of
$resource needs ngResource module.
$http is AngularJS core module.
$resource is specially designed for RESTful APIs and lets you write efficient, beautiful code when dealing with RESTful API. Hence, recommended in such scenarios over
$resource can deal with objects and arrays.
$http can be used to deal with primitive types as well.
$resource is recommended when writing complex queries and scalable functionalities.
$resource has a dependency on
$resource have a very similar functionality. But, it is recommended to use
$resource when you are dealing with RESTful APIs, because of the powerful programming interface provided by the
$resource service. And then, you can use
$http when you are dealing with non-RESTful APIs and services which respond with non-conventionals data types like strings.
If you are a beginner, I would recommend using
$http just to get a better understanding of how asynchronous programming is done in AngularJS. And then, when you start working on complex applications consider switching to the
I hope the article was worth your time. Please let me know if you have anything more to add to this.