Reputation: 2023
I am trying to use jquery in angular2 and got this error: ReferenceError: $ is not defined from the following code.
declare var $:JQueryStatic;
export class AppComponent {
ngOnInit() {
var container = $("#contact-us-form");
container.css("width", $(document).width()).css("height", $(document).height()).hide();
container.find(".modal-content .button-close").click(function(){ this.toggleModalWindow("contact-us-form"); });
$("#contact-link").click(function(){ this.toggleModalWindow("contact-us-form"); });
}
}
How could I resolve this ?
Upvotes: 4
Views: 17798
Reputation: 247
Try these Steps-
Upvotes: 0
Reputation: 3943
FOLLOW THESE STEPS:
npm i jquery --save
npm i @types/jquery -D
import * as $ from 'jquery'
---> in app.module.tsThat should do the trick and no need to put declare var jQuery: any;
or declare var $: any;
in each file.
Upvotes: 4
Reputation: 2023
add angular-cli.json
"../node_modules/jquery/dist/jquery.js"
Have a typescript file
import * as $ from "jquery";
install jquery using npm
Upvotes: 4