Skip to content

Instantly share code, notes, and snippets.

@nathanbarrett
Created December 9, 2017 18:29
Show Gist options
  • Select an option

  • Save nathanbarrett/4382a73dbe1365c162f060655204d4a1 to your computer and use it in GitHub Desktop.

Select an option

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
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