vxh.viet
7/4/2018 - 6:44 AM

Angular - How To Call External Javascript?

Angular - How To Call External Javascript?

SOURCE

IMPORTANT:

This method of having a definition script.d.ts doesn't work on Samsung Internet Browser up to version 7.4.00.70 and some Safari on older iOS.

Step 1. Create demo.js file in assets/javascript folder.

export function test1(){
    console.log('Calling test 1 function');
}

Step 2. Create demo.d.ts file in assets/javascript folder.

export declare function test1();

Step 3. Use it in your component

import { test1 } from '../assets/javascript/demo'; 
 @Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor() {
    console.log(test1());
  }
}

Note: js and .d.ts file name should be same