Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery with javascript in angular 2

    import {Component, ElementRef, OnInit} from 'angular2/core';
    declare var jQuery:any;
    @Component({
      selector: 'jquery-integration',
      templateUrl: './components/jquery-integration/jquery-integration.html'
    })
    export class JqueryIntegration implements OnInit {
      elementRef: ElementRef;
      constructor(elementRef: ElementRef) {
        this.elementRef = elementRef;
      }
      ngOnInit() {
        jQuery(this.elementRef.nativeElement).draggable({
        containment:'#draggable-parent'
      });
    }
  }

Can jQuery be used with TypeScript in Angular2 like the above? How do I use jQuery with JavaScript in Angular2?

like image 493
Buddhika Kulathilaka Avatar asked Jan 28 '16 06:01

Buddhika Kulathilaka


People also ask

Can I use both JavaScript and jQuery together?

Yes, they're both JavaScript, you can use whichever functions are appropriate for the situation. Save this answer. Show activity on this post. That kind of lower-level hacking around the DOM, messing with the inline handlers, and cross-browser quirks thereof, is precisely the reason jQuery was born.

Can I use jQuery and Angular together?

jQuery is a small yet feature-rich powerful javascript library that helps to manipulate the HTML DOM with less javascript code. We can use jQuery with Angular. There are some situations you come across while building Angular apps that it's absolutely necessary to use a library like jQuery to get something done.

Why jQuery is not used in Angular?

It adds a lot to bundle size which is very bad for slow networks and CPUs (mobile!). Selectors and events are usually solved by libraries like React and Angular, so you don't need jQuery to help with browser compability and API differences.


2 Answers

In my opinion: if you do it right and if you keep it minimal, you should go with that.

  1. Install jQuery Typings in project: tsd install jQuery
  2. Load jQuery with script tag ( or other loaders... )
  3. Wrap any jQuery plugin with Angular 2 Directive

Example:

@Directive({
  selector: "[sm-dropdown]"
})
export class SMDropdown {
  constructor(el: ElementRef) {
    jQuery(el.nativeElement).dropdown();
  }
}

Consider this Plunker:

https://plnkr.co/edit/MMNWGh?p=preview

Don't:

  • Don't use it for DOM manipulation, there is Angular way...
  • Don't use it for AJAX calls, there is Angular way...
  • Don't use it for animation, ngAnimation is coming...
like image 97
Vlado Tesanovic Avatar answered Oct 21 '22 06:10

Vlado Tesanovic


Once you have the JQuery typescript library installed, reference it in this way

///<reference path="../typings/jquery/jquery.d.ts" />

Then make the necessary imports

import {Component, AfterViewInit} from 'angular2/core';

Now write the class

@Component({
  selector: 'my-app',
  templateUrl: 'app/html/app.component.html'
})

export class AppComponent implements AfterViewInit {
  ngAfterViewInit() {
    // Your jQuery code goes here
    $('#here').text("HALLO! ^_^");
  }
}
like image 38
Saurabh Avatar answered Oct 21 '22 08:10

Saurabh