Is it a bad practice to use jQuery in Angular? [closed]
Asked Answered
O

3

15

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!

Oxford answered 29/11, 2018 at 8:43 Comment(4)
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.Espinal
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.Methanol
Possible duplicate of Should we use jQuery with AngularJS?Dissolve
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.Middlings
H
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.

Honey answered 29/11, 2018 at 9:3 Comment(1)
Why is it bad practice?Profile
P
5

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.

Propagandize answered 29/11, 2018 at 8:46 Comment(2)
Thank you, this made it a little bit clearer.Oxford
Welcome Dear . If this clear to you then you can mark it as answerd !Propagandize
I
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.

Ironing answered 29/11, 2018 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.