Adding a script to an Angular Library

Adding a script to an Angular Library


let scriptTap = document.createElement('script');
scriptTag.src = '<route to your scripts>';


The above shown method is the Basic method in Javascript to inject any scripts into you application. In Below example you can checkout the same injection method using angular provided methods.

Solution 2:

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

// Inject in constructor
constructor(private renderer: Renderer2, @Inject(DOCUMENT) private document: Document) {}

// Create Function to Inject Script
insertScript() {
    const js = this.renderer.createElement('script');
    js.type = 'text/javascript';
    js.src = 'https://somejavscriptfile.js';
    this.renderer.appendChild(document.body, js);


You can use above method and inject script in your code. This method uses a angular's inbuilt library known as Renderer2. You will also have to inject the Document via your constructor provided by Angular. This is required because Angular suggests you to not use Document and Window objects directly in Angular.

The reason for this is Angular is an Universal framework its can be used as an single app, it can be converted into a desktop app or even a native mobile application just by adding some additional libraries. And Document and Window objects are only available in browser, so it may occur that depending on environment angular code is being executed you may or may not have access to this Objects.

That's why its better to always inject Document object using constructor provided by Angular

πŸ”₯ 28 Views
Mar 18, 2022

Can't bind to 'formgroup' since it isn't a known property of 'form'

πŸ”₯ 67 Views
Mar 18, 2022

Angular gives error Can't bind to 'ngModel' since it isn't a known property of 'input' when you have not Imported FormsModule

πŸ”₯ 29 Views
Mar 18, 2022

To create a Directory that does not exist you have to use the --p command along with the mkdir command. Becausemkdir isn't recursive by default -- it expects the immediate parent directory to exist.

πŸ”₯ 3 Views
Apr 6, 2022

To create a folder in docker or a file create a directory for the build context and cd into it. You can use the classic mkdir command to create a folder in Docker.

πŸ”₯ 2 Views
Apr 6, 2022

Docker mkdir permission Failed ? For using data volumes in Docker by command-line and If your Docker host is on the Linux platform, you can find Docker volumes by /var/lib/docker/volumes path.

πŸ”₯ 2 Views
Apr 7, 2022

Docker Filesystems and Docker containers work in a similar manner to filesystems outside a Docker container. To provide users read/write/execute permissions use this command chmod 0777 /usr/local/docker/foo

πŸ”₯ 1 Views
Apr 6, 2022