0

//component 
import { ScriptService } from '../services/script.service';

constructor(private script: ScriptService) {

  }

  ngOnInit() {
    this.script.load('calenderly').then(data => {
      console.log('script loaded ', data);
    }).catch(error => console.log(error));
  }

I tried integrating calenderly in one component which actually worked when i visit the page for the first time but when I revisit the page from other page (route) it doesn't works.

//script store
interface Scripts{
    name:string;
    src: string;
}

export const ScriptStore:Scripts[]=[
    {name:'calenderly', src : 'https://assets.calendly.com/assets/external/widget.js'},
   
]

//script.service.ts
import { Injectable } from '@angular/core';
import { ScriptStore } from '../models/scriptstore'

declare var document: any;
@Injectable({
  providedIn: 'root'
})
export class ScriptService {

  private scripts: any = {};

constructor() {
    ScriptStore.forEach((script: any) => {
        this.scripts[script.name] = {
            loaded: false,
            src: script.src
        };
    });
}

load(...scripts: string[]) {
    var promises: any[] = [];
    scripts.forEach((script) => promises.push(this.loadScript(script)));
    return Promise.all(promises);
}

loadScript(name: string) {
    return new Promise((resolve, reject) => {
        //resolve if already loaded
        if (this.scripts[name].loaded) {
            resolve({script: name, loaded: true, status: 'Already Loaded'});
        }
        else {
            //load script
            let script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = this.scripts[name].src;
            if (script.readyState) {  //IE
                script.onreadystatechange = () => {
                    if (script.readyState === "loaded" || script.readyState === "complete") {
                        script.onreadystatechange = null;
                        this.scripts[name].loaded = true;
                        resolve({script: name, loaded: true, status: 'Loaded'});
                    }
                };
            } else {  //Others
                script.onload = () => {
                    this.scripts[name].loaded = true;
                    resolve({script: name, loaded: true, status: 'Loaded'});
                };
            }
            script.onerror = (error: any) => resolve({script: name, loaded: false, status: 'Loaded'});
            document.getElementsByTagName('head')[0].appendChild(script);
        }
    });
}

}
 <!-- Calendly inline widget begin -->
  <div class="calendly-inline-widget" data-url="https://calendly.com/prince-shah97/15min" style="min-width:320px;height:630px;"></div>
  
  <!-- Calendly inline widget end -->

strong textI have added dynamic js to my controller using the reference from medium "https://medium.com/better-programming/angular-load-external-javascript-file-dynamically-3d14dde815cb". It worked but only when I refresh the same page but not when I go the the page from the other route(page).

prince
  • 31
  • 4

0 Answers0