GitHub

DaffExternalScriptService

import { DaffExternalScriptService } from '@daffodil/core/external-script'

A service for loading external scripts into the document.

Usage example

@Injectable()
class DaffExternalScriptService implements DaffExternalScriptServiceInterface {
  readonly scriptMap: Map<string, LoadedExternalScript> = new Map()

  load(
    name: string
    script: DaffExternalScript
  ): Observable<boolean>
}

() Methods

load
Observable<boolean>
Parameters
Parametername: string
Description
Parameterscript: DaffExternalScript
Description

Examples

Loading an external script into the document

import { DOCUMENT } from '@angular/common';
import { inject } from '@angular/core';

import { DaffExternalScriptService } from '@daffodil/core/external-script';

const externalScriptService = new DaffExternalScriptService(inject(DOCUMENT));

externalScriptService.load('exampleScript', {
  src: 'https://example.com/script.js',
  async: true,
  defer: false,
  'data-custom-attribute': 'value',
}).subscribe({
  next: (result) => {
    console.log('Script loaded successfully:', result);
  },
  error: (error) => {
    console.error('Error loading script:', error);
  },
});