Should I use angularjs $ http service for requests or jquery ajax if possible?

In my project I am using angularjs framework and love to use the service $http

whenever I make an ajax call. But in some parts of the project, when the UI is not directly updated with an ajax call and angularjs bindings are not required, should I use a service $http

or a regular one jquery.ajax

?

More specifically, should I minimize angularjs dependencies in my project where the UI is not touching or tightly wraps the whole project with angular services and directives?

+3


source to share


4 answers


If you are using Angularjs then you should use $ http as your preferred method for making ajax calls. There is no need for this service to bind to the ui or affect the ui.

You should only use Jquery Ajax calls when you cannot perform the action with angular services, which will be rare if you follow the best techniques for working with angle signs.



Also, if you are using http services, you may have interceptors that create requests and processes before they receive the successful part of the call. They are used to handle global errors and display global notifications that, say, can come in any response. Alternatively, you can add headers and additional information before each request here. Also you can encode / decode the request / response here so that all these utility functions are done in the same place. Check the below link: http service doc with interceptors

If you still need to call $ http from the external angular environment, which should be avoided, execute a generic angular utility function and wrap it in some function that you can call directly.

+5


source


I personally would recommend maintaining it according to a generic service layer that encapsulates HTTP requests. While you can mix it up, I recommend doing this if you are dealing with code that has already been written with jquery. Also, if you want to change it later to update the interface, you will have to go back and change the jquery calls.



Another point that Blaze has already made is that $ http really shines if you're doing unit testing and want to ridicule the responses. Here's an example of this: http://www.unit-testing.net/CurrentArticle/How-to-mock-http-calls-in-Angular.html

+4


source


If the Ajax request doesn't interact with the UI, it doesn't really matter. $ http is like an Ajax request, the big difference is the $ http register in the digest loop, which updates the UI with the new data. If there is no need to update the user interface, you can use whatever makes you happy.

0


source


You should use the Angular framework, which she highly recommends. There's a good introduction for jQuery developers in Angular http://www.ng-newsletter.com/posts/angular-for-the-jquery-developer.html

Discusses the differences between $ http vs $ .ajax Angularjs $ http VS jquery $ .ajax from jquery $ .ajax to Angular $ http

Moreover, using jQuery with Angular can lead to some problems, eg. https://github.com/angular/angular.js/issues/2548 (jQuery causes ngClick to malfunction with touch devices)

0


source







All Articles