Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

How to install and use jQuery in Angular 6

Olivia Rhye

First run

$ npm install jquery –save

This will add jquery to the package list and make it a dependency. Next in your componentname.component.ts file add

“import * as $ from ‘jquery';"

jQuery has now been added to the component and can be used from this location. Write all jQuery within the ngOnInit() { } function and you will now see it running on your page.

Example component.ts file:



import { Component, OnInit } from '@angular/core';

import * as $ from 'jquery';



@Component({

selector: 'app-componentname',

templateUrl: ‘./componentname.component.html',

styleUrls: ['./componentname.component.scss']

})

export class ComponentnameComponent implements OnInit {

constructor() { }

ngOnInit() {

$(document).ready(function () {

$(‘.selector’).addClass(‘working’);

});

}

}

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy