1

I want to use jquery and easypiechart js file's functions in typescript.

It doesn't work this way.

How to define these script what i specified in code as typescript ?


index.component.ts

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

import * as $ from "../../../../../assets/plugins/jquery/jquery.min.js";
import { easyPieChart } from "../../../../../assets/plugins/easypiechart/jquery.easypiechart.min.js";

// these above 2 js files are defined in angular.json script section

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

export class IndexComponent implements OnInit {

  constructor() {}

  ngOnInit() {

    //$(function(){
    //  $('.easypiechart').easyPieChart();
    //});

    // How to write this above script as typescript ?????????????????????

  }
}
4
  • 1
    This looks dublicate of this. stackoverflow.com/questions/47346559/… Commented Feb 5, 2019 at 3:56
  • as pointed out it`s a duplicate also why would you use jquery in angular? any specific reason for that? Commented Feb 5, 2019 at 4:08
  • Don't use jquery in angular. Choose one framework or the other Commented Feb 5, 2019 at 4:09
  • Possible duplicate of using external JS libraries in my angular 2 project Commented Feb 5, 2019 at 4:39

4 Answers 4

2

From the above question,it looks like jquery.easypiechart.min.js is the one that you need to use in your angular application as external js.

  1. Put the js under assets folder say /assets/js/jquery.easypiechart.min.js
  2. Goto your projects angular.json file and under scripts node of architect node put as an entry in the array.

    "scripts": [ "./node_modules/jquery/dist/jquery.min.js",
    "./src/assets/js/jquery.easypiechart.min.js" ]

Now you can refer the external js in any of your projects components

  declare var $: any;// referencing jQuery library
    @Component({
      selector: 'app-index',
      templateUrl: './index.component.html',
      styleUrls: ['./index.component.scss']
    })

    export class IndexComponent implements OnInit {
      constructor() {}
      ngOnInit() {
      $(document).ready(function () {
          //accessing easypiechart.min.js.
          $('.easypiechart').easyPieChart();
         });
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

If you have included them in the scripts or index.html, you don't have to import them to the .TS file again

Use declare instead and it should work What does 'declare' do in 'export declare class Actions'?

Comments

0

Instead of putting it in asset folder you should use it as node_modules dependency

For easy pie chart run this npm i easy-pie-chart --save & for jquery run npm i jquery

Comments

0

Normally you don't want to use jquery in Angular, because it usually implies to modify directly the DOM, which is a bad practice, but there is the way to do it: https://medium.com/all-is-web/angular-5-using-jquery-plugins-5edf4e642969

If you wanna plot a pie chart or other types of charts, you could use ng2-charts instead, it will allow you to use charts.js with Angular and Typescript.

Comments

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.