Skip to content

angularcity/external-js-angular5

Repository files navigation

Steps followed

In assets/dummy-data.js, i have a normal function.

function greetings(){
 return {
   data: "Greetings from External JS"
 }
}

In tsconfig.json

"allowJs": true,

In angular-cli.json

"scripts": [
   "./assets/dummy-data.js"
 ],

If you check the Source tab, scripts.bundle.js, that is already a part of the final bundle. alt text

In whichever component

import { Component, OnInit, AfterViewInit, AfterContentInit } from '@angular/core';
declare var greetings: any; // declare with the name of the function 
 
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
 title = 'app';
 message;
 ngOnInit(): void {
   this.message = greetings(); // use it like this in any life cycle hook
 }
}

In html

<h1> Message is {‌{ message.data }} </h1>

Output alt text

About

sample integration of an external js file with angular 5

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •