Created
December 9, 2017 18:29
-
-
Save nathanbarrett/4382a73dbe1365c162f060655204d4a1 to your computer and use it in GitHub Desktop.
A helper service for async loading of libraries such as google maps , google charts , etc
This file contains hidden or 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'; | |
| import Promise from 'bluebird' | |
| @Injectable() | |
| export class DomScriptLoaderService { | |
| constructor() { } | |
| /** | |
| * Returns a Promise with a boolean value as to whether is was previously loaded or not | |
| * @param {string} url | |
| * @returns Promise<boolean> | |
| */ | |
| load(url: string): Promise<boolean> { | |
| return new Promise(resolve => { | |
| if (!document.querySelectorAll(`[src="${url}"]`).length) { | |
| document.body.appendChild(Object.assign( | |
| document.createElement('script'), { | |
| type: 'text/javascript', | |
| src: url, | |
| onload: () => resolve(false) | |
| })); | |
| } else { | |
| resolve(true) | |
| } | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment