Angularjs $resource vs $http… Which one do you use?
Some Answers I found:
- $http is for general purpose AJAX for use with GET, POST, DELETE etc…
- $resource wraps $http for use in RESTful web API scenarios…
- $resource is just a higher abstraction on the top of $http, so if $resource fits your needs, use it.
- Even if you are using $resource, you can use $httpBackend mock for testing it.
- $resource talks to $http, $http talks to $httpBackend (which you mock out during unit test).
- Check out https://docs.angularjs.org/api/ngMock.$httpBackend
The best Answer I found:
I think putting $http requests into a service just generally works out better because you want to have access to the data from multiple locations and the service acts as a singleton so basically you can handle any kind of caching you want to do there and controllers can all just watch the appropriate services to update their own data. I’ve found that a combo of $watch in the controllers for data on the service and returning the promises from my service’s methods gives me the most flexibility with how to update things in the controller.