Created
March 6, 2019 07:23
-
-
Save Anj21/3fabe7b8ba4b617f962b656867bc1aab to your computer and use it in GitHub Desktop.
load external java script file dynamically after navigation in angular2+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { LoadJsService } from './load-js.service'; | |
@Component({ | |
selector: 'app-home', | |
templateUrl: './example.component.html', | |
styleUrls: ['./example.component.css'] | |
}) | |
export class HomeComponent implements OnInit { | |
constructor(private jsObj: LoadJsService) { | |
} | |
ngOnInit() { | |
this.loadScripts(); | |
} | |
private loadScripts() { | |
this.jsObj.load('customMinJs').then(data => { | |
}).catch(error => console.log(error)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
interface Scripts { | |
name: string; | |
src: string; | |
} | |
export const ScriptStore: Scripts[] = [ | |
{name: 'customMinJs', src: './assets/js/scripts/custom.min.js'} | |
]; | |
declare var document: any; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class LoadJsService { | |
private scripts: any = {}; | |
constructor() { | |
ScriptStore.forEach((script: any) => { | |
this.scripts[script.name] = { | |
loaded: false, | |
src: script.src | |
}; | |
}); | |
} | |
load(...scripts: string[]) { | |
const promises: any[] = []; | |
scripts.forEach((script) => promises.push(this.loadScript(script))); | |
return Promise.all(promises); | |
} | |
loadScript(name: string) { | |
return new Promise((resolve, reject) => { | |
if (!this.scripts[name].loaded) { | |
console.log("if of first if") | |
//load script | |
let script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.src = this.scripts[name].src; | |
if (script.readyState) { //IE | |
console.log("second of first if") | |
script.onreadystatechange = () => { | |
if (script.readyState === "loaded" || script.readyState === "complete") { | |
script.onreadystatechange = null; | |
this.scripts[name].loaded = false; | |
resolve({script: name, loaded: true, status: 'Loaded'}); | |
} | |
}; | |
} else { //Others | |
console.log("else of second if") | |
script.onload = () => { | |
this.scripts[name].loaded = false; | |
resolve({script: name, loaded: true, status: 'Loaded'}); | |
}; | |
} | |
script.onerror = (error: any) => resolve({script: name, loaded: false, status: 'Loaded'}); | |
document.getElementsByTagName('head')[0].appendChild(script); | |
} else { | |
console.log("else of first if") | |
resolve({ script: name, loaded: true, status: 'Already Loaded' }); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment