-1

I have added an external js file to my angular14 solution. The JS file has $('.class').click() event. Whenever I run the application in localhost, the browser says $ is not defined. I am new to angular and my question is how can I properly define $?

I tried adding var $ = require('jquery') in the first line of js file but it produces new error saying require is not defined.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Quick suggestion: If you're starting a new Angular project, just stop using jQuery at all and use proper Angular component binding. That and external JavaScript files are likely to cause problems if they mess with the Angular-managed DOM. – Alejandro May 17 '23 at 17:31
  • That said, if you *really* want to do this, try importing jQuery with a ` – Alejandro May 17 '23 at 17:32

2 Answers2

0

It seems you are trying to use jQuery in your Angular application.

$ is a shortcut for jQuery.

When you are developing an Angular application, there usually is no need to use also jQuery library. But if there still is a case for using jQuery in your app alongside Angular, have a look at this post: How to use jQuery with Angular?

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
0

Thanks for your replies, so I found out the mistake I did. I was able to include my external JS to scripts in angular.json. That should be enough however, I also placed <script src=""></script> in the index.html. I just removed it and the error is gone. I was doing a some sort of Form Slider and I need the js to handle the animations for me.