4

How I can use jQuery plugin with Angular 2 app?

For example: circliful (https://github.com/pguso/jquery-plugin-circliful).

All I know is that I have to import jQuery and the plugin .js into my index.html, but how I can place a code like this (shown in the plugin documentation):

<script>
$( document ).ready(function() {
        $("#your-circle").circliful({
        animationStep: 5,
        foregroundBorderWidth: 5,
        backgroundBorderWidth: 15,
        percent: 75
    });
    });
</script>

into an angular 2 app - I have no idea!

2
  • This is essentially the same as asking how to install a Model T carburetor in a Ferrari. Commented Jun 29, 2016 at 15:51
  • not as answer, but if somebody will look for solution, here is a nice article about it: radzen.com/blog/jquery-plugins-and-angular Commented Apr 18, 2017 at 18:59

2 Answers 2

1

You had better not to use jquery and use the directive like circle progress bar by the way, but its possible to write a directive which uses a jquery plugin.

There are three kinds of directives in Angular but you need to use Attribute directives to embed this library like the following code

import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
    selector: '[circliful]'
})
export class CirclifulDirective {
   constructor(private el: ElementRef) { 
       $(el).circliful({
           animationStep: 5,
           foregroundBorderWidth: 5,
           backgroundBorderWidth: 15,
           percent: 75
       });
   }
}

and then you could use this directive.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that ElementRef returns undefined for DOM elements in the template within an *ngIf. Use [hidden] instead
0

You need to use both javascript files and definition files to run jquery. To add the javascript files just put them in the index.html. To add definition files isn't required, but it's a good idea, because in the TS domain you'll be able to autocomplete and check jquery methods. If you want to use definition files (d.ts) you need to install, using npm, tsd and use tsd to install jquery.d.ts file.

2 Comments

Nothing here about a jquery library
I think a better approach is place this script on component initialization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.