6

While searching for "showing bootsrap modal in angular 2" I encountered following answer: https://stackoverflow.com/a/38271918/1291122

It simply declares jQuery as this:

declare var jQuery:any;

And uses it to show/hide modal like this:

jQuery("#myModal").modal("hide");

This was the shortest way to achieve what I need in angular 2(other all answers seemed to make it fairly complex equivalent to rocket science!)

While this is the shortest way, is it the recommended way to do it? And in general is it a good idea to use jQuery with angular 2+?

EDIT:

My question is different from How to use jQuery with Angular2? Because I am asking WHETHER(or not) to use jQuery with angular 2, while that question is about HOW to use jquery with angular 2. I already know and have mentioned How to do it.

rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • 2
    You can use jQuery, but first ask yourself do you really want this? jQuery works with the physical DOM and adds dependency in to your bundle, which makes it heavier (loading longer). – Alexander Surkov Jun 16 '17 at 13:21
  • 1
    @AlbertGore yeah thats correct. Angular and jquery are two very different concepts. While jquery manipulates DOM directly, angular is about data bindings etc.. However in scenarios where you can get something done quickly using a jquery library, I think we need to evaluate the trade offs. – rahulserver Jun 16 '17 at 13:31
  • As alternative way, you can use some ready angular2+ components for modal window. – Alexander Surkov Jun 16 '17 at 13:48
  • Possible duplicate of [How to use jQuery with Angular2?](https://stackoverflow.com/questions/30623825/how-to-use-jquery-with-angular2) – Daniel Beck Jun 16 '17 at 14:01
  • Suppose I need to query a lot of tags in SVG. Maybe machine-generated SVG even with lots of nodes. So I don't bind but rather query the tags procedurally. It's obvious that Angular's binding does not always fit well, right. And I could use Zepto to spare some kilobytes. – Gherman Sep 22 '17 at 06:48

2 Answers2

4

Yes you can use it without any problem.

This is the fastest solution, but you can have an even better one in this topic. Basically, it gives a way to have all the methods with the IDE auto complete.

2

I don't see a downside. Especially in cases where you use external libraries. However, just don't revert to Jquery trying to solve angular problems like templating etc.

Akk3d1s
  • 211
  • 3
  • 8