Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular dynamic script loading

import { Injectable } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { Observer } from "rxjs/Observer";

@Injectable()
export class ScriptLoaderService {
    private scripts: ScriptModel[] = [];

    public load(script: ScriptModel): Observable<ScriptModel> {
        return new Observable<ScriptModel>((observer: Observer<ScriptModel>) => {
            var existingScript = this.scripts.find(s => s.name == script.name);

            // Complete if already loaded
            if (existingScript && existingScript.loaded) {
                observer.next(existingScript);
                observer.complete();
            }
            else {
                // Add the script
                this.scripts = [...this.scripts, script];

                // Load the script
                let scriptElement = document.createElement("script");
                scriptElement.type = "text/javascript";
                scriptElement.src = script.src;

                scriptElement.onload = () => {
                    script.loaded = true;
                    observer.next(script);
                    observer.complete();
                };

                scriptElement.onerror = (error: any) => {
                    observer.error("Couldn't load script " + script.src);
                };

                document.getElementsByTagName('body')[0].appendChild(scriptElement);
            }
        });
    }
}

export interface ScriptModel {
    name: string,
    src: string,
    loaded: boolean
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react usestate vs variable 
Javascript :: js return vs break in for loop 
Javascript :: Invariant Violation: [React Intl] An `id` must be provided to format a message. 
Javascript :: mongoose findone exclude own document 
Javascript :: equals sign vue js 
Javascript :: how to make password star star on input html 
Javascript :: how to print date in javascript without time 
Javascript :: react native icon onpress remove highlight onpress 
Javascript :: javascript to python converter online 
Javascript :: about react frame 
Javascript :: point towards mouse wick editor 
Javascript :: react first click not working 
Javascript :: how to detect clicks using javascript addeventlistener 
Javascript :: javascript onclick event add html element 
Javascript :: type.js 
Javascript :: material icon button ripple 
Javascript :: read data from store i ngrxstore 
Javascript :: readfle nodejs 
Javascript :: random number from 1 to 10000 js 
Javascript :: How to set variable data in JSON body for the code that generated by Postman in c# 
Javascript :: any-text npm 
Javascript :: document.body.insertBefore 
Javascript :: disable button without losing value 
Javascript :: Remove a class when the backspace-key is pressed inside the input field 
Javascript :: ist to gmt javascript 
Javascript :: mengakses gambar didalam asset angular 
Javascript :: component rerender for tab navigation 
Javascript :: option 1 
Javascript :: nodejs express use streams 
Javascript :: convert promise to generator js 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =