Last Updated: September 09, 2019
· filso

Why you should use AngularJS with jQuery

Myth of no need to use jQuery with AngularJS apps spreads around like a virus.

Why you probably should use jQuery along with AngularJS:

jQuery serves different purpose than AngularJS. AngularJS is a framework that gives structure for your app. jQuery is a DOM manipulation library that abstracts browser API differences.

AngularJS uses jQuery / jqLite underneath for it's native directives (all ng-*), cause jQuery does DOM manipulation well. That's why AngularJS creators decided to use jQuery to do that in the first place.

So yes, you should use jQuery in your AngularJS app. What is bad is doing DOM manipulation (using jQuery or whatever) inside controllers / services. You should always do DOM manipulation only in directives.

Below I pasted contents of ng-style directive. As you can see it's using jQuery (or stripped version - jqLite):

var ngStyleDirective = ngDirective(function(scope, element, attr) {
  scope.$watch(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
   if (oldStyles && (newStyles !== oldStyles)) {
       forEach(oldStyles, function(val, style) {         
     element.css(style, '');
    if (newStyles) element.css(newStyles);
  }, true);

If you need to do any DOM manipulation that ng-* don't provide, you could also benefit from using jQuery to abstract browser differences and focus on your problem. There's nothing wrong with it.

The biggest functional overlap of jQuery and AngularJS are events, and a few utility functions. As for the events I recommend using AngularJS events to easily control lifescope of components, as for the latter Lodash seems most useful.

2 Responses
Add your response

Yeah, but including full jQuery library in your web application just because you want to use $element.css('display', 'block') instead of native DOM functions like = 'block' is silly.

jqLite, angular's helpers, angular's $http, angular's Promise and native DOM functions like document.querySelectorAll(selector) (available on every today's browser - IE8 isn't supported by Angular 1.3 anymore anyway) are often just enough and jQuery doesn't add anything new.

I agree jQuery might be useful but IMHO you should include it only when you don't care about your JS size (f.e. native crossplatform apps) or when you really need some of its' functions f.e. for some external libraries.

over 1 year ago ·

jtomaszewski: agreed with all. Yet, that's not really jQuery vs AngularJS debate. That's a debate, whether jQuery solely for DOM manipulation is useful at all, whether we use any framework on top of it, or not.

I really like jQuery, it makes me more productive, focus on domain problem at hand instead of thinking what was the name of a native JS method. On the other hand, I see how it can be an issue with many projects. One is what you mentioned (JS size), the other is speed.

In the end, I guess one has to choose technology for each project individually, based on requirements, and most of these technology flame wars are like "apples vs oranges" discussions ;)

over 1 year ago ·