How to call JavaScript functions from Typescript in Angular 5?

后端 未结 2 1977
谎友^
谎友^ 2020-11-28 23:48

I\'m working on PDF Viewer development in Angular 5. I\'m done with writing HTML code for the UI part. Now I\'ve JavaScript files that provide functionality for the UI eleme

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 00:25

    The given answer by Arun Raj R is perfect.

    But for the angular 6+ project you need to take angular.json instead of angular-cli.json.

    also if you want to pass parameter inside external function that come from js file than use just function name.

    For Ex :

    app.component.ts

    import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
    declare function myfunction: any; // just change here from arun answer.
    
    @Component({
      selector: 'app-test',
      templateUrl: './test.component.html',
      styleUrls: ['./test.component.scss']
    })
    export class TestComponent implements OnInit {
        constructor() { }
    
        ngOnInit() { 
            myfunction('test1', 'test2');
        }
    };
    

    yourJs file :

    function myfunction(params1, params2) {
        console.log('param1', params1);
        console.log('param2', params2);
    }
    

提交回复
热议问题