What is the difference between $http and $resource? - angularjs

Can I use either for a REST call ? Or is there some minor difference ? I have used both of them in my projects with no side effects,

These are the main differences between $http and $resource:
$http:
$http is for universal purpose. It is an Ajax call.
$http is built into the AngularJS framework.
$http is good for quick retrieval.
$http is injected directly into an AngularJS controller by the developer.
For more details about $http refer: https://docs.angularjs.org/api/ng/service/$http
$resource:
$resource wraps $http to use in RESTful web APIs.
$resource needs to add the module separately.
$resource is good for conditions slightly more complex than $http.
$resource does not allow us to do much with data once it is consumed in the application. It is in the final state when it is delivered to the HTML DOM. The data are the same that the $http method will receive.
For more details about $resource refer: https://docs.angularjs.org/api/ngResource/service/$resource
You can find more answer here and here.

Related

Difference between xmlhttp (js) and $http (AngularJS)

In normal JavaScript we use xmlhttp for Ajax. Angular is providing ajax service using $http. Is there anything extra things in $http compare to xmlhttp?
$http in AngularJS cannot be configured to be synchronous while xmlhttp can do either synchronous or asynchronous. In AngularJS world, we use $http because it is the "Angular way" of doing requests.

$httpInterceptor not triggering before bootstrapping

Currect Structure
I'm Bootstrapping my angular application manually because I have to get some data before booting my angular app, which uses $http & injected it manually using:
var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
& use $http to call api
I also have an http interceptor which uses $httpProvider to handle specific errors in app.config function.
$httpProvider.interceptors.push('HttpInterceptor');
The Problem
Consider the api call before bootstrapping, the http interceptor handling is not working. I think because the factory i'm passing the $httpProvider is not yet tied up & will be initialised after the bootstrapping is done.
So Tell Me
Should I handle error case manually for that particular http request(before bootstrapping) which I think would be redundant or Is there a way I can make tie the httpInterceptor work before bootstrapping.
What would be a better approach?

Angular resource and $http.put

What's the difference between using $resource to make a put request and using $http.put to make a put request? Does it make any difference in the speed or performance?
Check Answers in this stackoverflow thread
$resource is further high-level abstraction of $http. I don't think $resource added significant performance burden on top of $http to get the high-level API .

Angularjs - Why mock $httpBackend and not $http

The Angularjs tutorial shows something using the $http service and then testing that using the $httpBackend mock. What it doesn't explain is why you mock $httpBackend and not just mock the $http service itself? Can anyone shed light of this?
You don't mock $httpBackend. You use it to mock the actual HTTP requests that $http makes. I suppose you probably could just mock $http itself, but $httpBacked provides a lot of functionality for asserting certain requests are made (The expect methods) and for just dummying in a response (The when methods). In short, $httpBacked makes testing code that uses $http much much easier.

Good Practices of AngularJS

After searching for hours in google and stackoverflow, I did not get the answer for good practices for AngularJS.
My Questions Are ::
1) Is it a good practice to manipulate a DOM using JQuery and CSS in AngularJS?
2) When to add $injector and $inject explicitly?
3) Is it good practice to use JQuery's $.ajax() method to call the asynchronously in the controller of a Module?
1) Is it a good practice to manipulate a DOM using JQuery and CSS in AngularJS?
You may be surprised how much Angular can do without jQuery. However, jqLite is certainly a "lite" version of jQuery. If you can't do it "clean" in Angular (e.g., if you find yourself writing parent().parent() instead of closest('.element-wrapper')) then sure, reach for jQuery.
2) When to add $injector and $inject explicitly?
Normal dependency injection is usually sufficient. You could, however, dynamically inject a service using the $injector, if you really need to: AngularJS dynamically inject scope or controller
3) Is it good practice to use JQuery's $.ajax() method to call the asynchronously in the controller of a Module?
Use $http or $resource. They do things $.ajax() doesn't, e.g., they'll initiate an AngularJS digest cycle when results come back from the server. Often, you'll want to put your server interaction code into an Angular service.

Resources