14

My question is the following. Should I avoid using any kind of jQuery code in Angular application as it seems legit to have only one thing interacting with DOM. Another question is if anyone came across problems where he couldn't find any other solution but writing a quick hack with jQuery.

Thank YOU!

Davit Karapetyan
  • 589
  • 1
  • 6
  • 14
  • 2
    i'd say it's bad practice to use either but that's just my opinion. also this question is pretty opinionated and does not really fit stackoverflow. – GottZ Nov 29 '18 at 08:45
  • 2
    It adds a lot to bundle size which is very bad for slow networks and CPUs (mobile!). Selectors and events are usually solved by libraries like React and Angular, so you don't need jQuery to help with browser compability and API differences. One could say that it's an opinion but I dare to say that it's straight up bad to use both. – Solo Nov 29 '18 at 08:46
  • 3
    Possible duplicate of [Should we use jQuery with AngularJS?](https://stackoverflow.com/questions/30007792/should-we-use-jquery-with-angularjs) – holydragon Nov 29 '18 at 08:47
  • 1
    You should never include jQuery in an AngularJS app. Doing so is bad practice. However, if you are using some oldschool jQuery plugin wrapped into directives, and by that have jQuery included, it is perfectly OK to use jQuery when you deal with the plugin under the hood, i.e need to target properties etc. There are a lot of "religious" people on SO telling you not to use jQuery, but in fact most of them do not realize they are using jQuery themselves, for example if they are using frameworks like bootstrap. Short: If you not **need** jQuery as a dependency, then you should not use jQuery. – davidkonrad Nov 29 '18 at 09:04

3 Answers3

13

Yes it's a bad practice, but sometimes it will save you much time, especially when you are looking for a plugin, Do it when necessary only, and keep a note to switch it back when other solutions are available.

amd
  • 20,637
  • 6
  • 49
  • 67
4

The first thing you should do is to read this thread on SO "Thinking in AngularJS" if I have a jQuery background?. This will give you some perspective.

When it comes to Angular, it the model that drives the view and most of the times direct DOM manipulation is not required.

For example if you are using DOM manipulation to show\hide element, add remove class or set style, then better to use ng-show\ng-class\ng-style directive.

But there are cases when DOM manipulation is required and that is the time you write directives and either use jqLite or jQuery to manipulate DOM.

My suggestion would be to avoid jQuery unless you have to incorporate a jquery plugin that is dependent on jQuery.

While developing always look if the inbuilt directives that can serve your purpose. If not can jqLite be used to achieve what is desired. Your final resort should be jQuery.

MD. Jubair Mizan
  • 1,539
  • 1
  • 12
  • 20
2

Well it's just two large resources, which makes your app "heavy". Otherwise it's only a preference thing. Personally I don't use jQuery with any of the reactive frameworks (Vue, React nor Angular).
Remember that anything jQuery can do, you can do with vanilla JS.

Borisu
  • 828
  • 7
  • 15