Home Angular7 Add and Run Jquery in Angular 7

Add and Run Jquery in Angular 7

by therichpost
20 comments
angular7

Hello to all, welcome to therichpost.com. In this post, I will tell you, Add and Run Jquery in Angular 7.

Angular is growing very fastly and popular as single page application. Today I am working with Angular 7 and wanted to run some jquery methods and I succeed and I am sharing that tricks what I did on my app.

I have written so many post related to jQuery because jQuery is my first love and today I also use jQuery in my many projects.

Here is the working image, I added dynamic class to body tag with jQuery:

 

Add and Run Jquery in Angular 7

 

Here is the working and tested code and you need to follow and into your Angular 7 application:

 

1. Very first, you need to run below command into your terminal :

$ npm install jquery --save

 

2. Now, you need to add below jquery file path into your Angular 7 application angular.json file:

 

....

"scripts": ["node_modules/jquery/dist/jquery.js"]
....

 

3.  Now you need to add below code into your app.component.ts file:

import * as $ from 'jquery';

export class AppComponent {

  ngOnInit() {

  $('body').addClass('df');

  }

}

4. In this end run your Angular 7 application and you will see, new class will be added to body tag.

if you have any query related to this post, then please do comment below or ask question.

Thank you,

Alisha,

TheRichPost.

 

 

You may also like

20 comments

paparao November 20, 2018 - 6:45 am

i did all steps what u said,
but got errors when i aam column resizing using jquery .
the error llike
TypeError: jquery__WEBPACK_IMPORTED_MODULE_1__(…).css(…).prepend(…).resizable is not a function

,
Uncaught ReferenceError: jQuery is not defined

my code is
—————
import { Component, OnInit } from ‘@angular/core’;
import * as $ from ‘jquery’;

@Component({
selector: ‘app-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [‘./app.component.css’]
})
export class AppComponent implements OnInit{
title = ‘tables’;

ngOnInit() {
alert(“$ vesion is “+jQuery.fn.jquery)
//$(“td,th”)
$(“td:first-child,td:nth-child(2),td:nth-child(3)”)
.css({
/* required to allow resizer embedding */
position: “relative”
})
/* check .resizer CSS */
.prepend(“”)
[‘resizable’]({
resizeHeight: false,

// we use the column as handle and filter
// by the contained .resizer element
handleSelector: “”,
onDragStart: function(e, $el, opt) {
// only drag resizer
if (!$(e.target).hasClass(“resizer”))
return false;
return true;
}
});

}
}

html code is
—————-

Drag and Resize

Table Head
Table Head
Table Head
Table Head

Table Data
Table Data
Table Data
Table Data 12412412414124

Reply
Ajay Malhotra November 20, 2018 - 3:08 pm

HI Paparao, it seems like, you are jquery ui elements so you’re getting this error.
Try to use :

declare let $: any;
instead of
import * as $ from ‘jquery’;

———-or ————-

1 : Import jquery-ui-dist
npm install jquery jquery-ui-dist

2: Add scripts in angular-cli.json
“scripts”: [
“../node_modules/jquery/dist/jquery.min.js”,
“../node_modules/jquery-ui-dist/jquery-ui.js”
],

Thank you

Reply
moustafa youssif February 2, 2019 - 6:40 am

That’s nice , great idea I will try to use it because you know not all UI plugins have angular7 implementation yet . So you are my hero ?

Reply
Ajay Malhotra February 3, 2019 - 5:17 am

Thank you moustafa

Reply
dacheng March 18, 2019 - 6:22 am

ok it goes well
Thanks for your proposal

Reply
Ajay Malhotra March 19, 2019 - 3:22 pm

Great..

Reply
safa March 20, 2019 - 3:20 pm

hello i want to ask if you tried to use Mapster or maphilight in Angular 7 to highlight areas in an image , as i am currently working on this and i faced some issues .
Thank you

Reply
Ajay Malhotra March 20, 2019 - 4:34 pm

You can show me your working example and you can tell me the issue coming in console?

Reply
safa March 21, 2019 - 8:07 am

This is the HTML code : it is for image mapping :

This is the component.ts file :

import {OnInit,Component} from ‘@angular/core’;
import { HttpClient } from ‘@angular/common/http’;
import * as jQuery from ‘jquery’;
declare var $: any;

@Component({
selector: ‘app-maphilight’,
templateUrl: ‘./maphilight.component.html’,
styleUrls: [‘./maphilight.component.scss’]
})

export class MaphilightComponent implements OnInit {
title = ‘web_ui’;
State: JSON;
ngOnInit(){
$(“map[name=map] area”).on(“click”, function(e){
e.preventDefault();
alert($(this).attr(‘coords’));
});

$(‘img’).mapster({mapKey: ‘room’});

}

there is no error in Console but this is not working correctly, because each area should be highlighted onclick , like in this example : http://jsfiddle.net/jamietre/jQG48/

I am using the latest version of Jquery .
Thank you for your time

Reply
safa March 21, 2019 - 8:22 am

.

Reply
safa March 21, 2019 - 8:27 am

Actually i am new to Angular …
this is my html code for mapping the image

and this is the component.ts

import {OnInit,Component} from ‘@angular/core’;
import { HttpClient } from ‘@angular/common/http’;
import * as jQuery from ‘jquery’;
declare var $: any;

@Component({
selector: ‘app-maphilight’,
templateUrl: ‘./maphilight.component.html’,
styleUrls: [‘./maphilight.component.scss’]
})

export class MaphilightComponent implements OnInit {
title = ‘web_ui’;
State: JSON;
ngOnInit(){
$(“map[name=map] area”).on(“click”, function(e){
e.preventDefault();
alert($(this).attr(‘coords’));
});

$(‘img’).mapster({mapKey: ‘room’});

}

i wanted to use mapster API to trigger highlight or selection onclick on each area like in this example http://jsfiddle.net/jamietre/nM7kq/

and i am not getting any error but it doesn’t work correctly , onclick nothing is selected or highlighted even when i remove the alert giving the coordinates.

Thank you in advance for your time

Reply
Ajay Malhotra March 21, 2019 - 9:18 am

you can email me : therichposts@gmail.com

Reply
Ajay Malhotra March 21, 2019 - 9:22 am

No need to use: import * as jQuery from ‘jquery’;

Also you need to add mapster also.

Reply
safa March 25, 2019 - 1:56 pm

Thank you , You’re right the error was that i haven’t added the mapster plugin and it is fixed now.

Reply
Ajay Malhotra March 25, 2019 - 3:05 pm

wow that is great..

Reply
yusuf May 27, 2019 - 6:24 am

thank you
short and useful post

Reply
Ajay Malhotra May 27, 2019 - 4:01 pm

Thank you Yusuf

Reply
marcel July 31, 2019 - 3:42 pm

hello, I am loading some libraries external to my project but so far I can’t make it work,

my error is TypeError: jquery__WEBPACK_IMPORTED

Reply
Ajay Malhotra July 31, 2019 - 4:12 pm

Define jquery global and it will work. Thanks! –

Reply
Julien Pitt November 18, 2019 - 9:28 am

great

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.