dynamic-script-injector
v1.0.6
Published
This Angular Module allows you to dynamically add scripts to the index.html
Downloads
2
Maintainers
Readme
Dynamic-Script-Loader
This Angular Module allows you to dynamically inject scripts into the index.html (top level) and initializes the script.
This allows for the usage of javascript libraries without the need to add them to your angular.js
in the scripts: []
section.
Installation
npm install dynamic-script-loader
Scaffolding
Import the module into your project under imports
imports: [
BrowserModule,
AppRoutingModule,
DynamicScriptInjectorModule
],
Use
Import the service into your component
import { DynamicScriptInjector } from 'dynamic-script-injector';
Import the service into the constructor
private scriptInjector: DynamicScriptInjector
Loading a Single Script
You can load a single script using the following
const sScript = ""
load("./samplefolder/samples/script.js")
Loading Multiple Scripts
For multiple script loading, create an array of script paths
const scripts = ["./samplefolder/samples/script-1.js", "./samplefolder/samples/script-2.js"]
loadAll(scriptUrls: string[])
Here is a sample of a component setup
export class AppComponent implements OnInit {
constructor(
private scriptInjector: DynamicScriptInjector
) {}
ngOnInit() {
const scripts = ["./samplefolder/samples/script-1.js", "./samplefolder/samples/script-2.js"]
loadAll(scriptUrls: string[])
}
}