Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
angular-lazyscripts
Advanced tools
lazy load external js scripts and libraries in your angular projects. this module will handle loading the script you need before you use it in your components.
lazy load external js scripts and libraries in your angular projects. this module will handle loading the script you need before you use it in your components.
npm i -S angular-lazyscripts
AngularLazyscriptsModule
Import AngularLazyscriptsModule
from the angular-lazyscripts
package, and add it to the NgModule
imports array of your lazy loaded module or the module you need.
import { NgModule } from '@angular/core';
...
import { AngularLazyscriptsModule } from 'angular-lazyscripts';
@NgModule({
...,
imports: [
...,
AngularLazyscriptsModule,
...
]
...
})
export class MyLazyModule {}
alsLoad
structural directiveAdd the alsLoad
directive with library url (any url you would use in a script tag on your index.html is fine) to your component tag, or one of its parent tags:
<myComponent *alsLoad="'https://cdn.something.net/somelibrary.js'">
<!-- this component uses alsLoad directive to load after the script has been loaded -->
</myComponent>
<div *alsLoad="'https://cdn.something.net/somelibrary.js'">
<!-- this div and it's content will be added to the dom after somelibrary.js is added and loaded. if loading fails or it takes longer than 15 seconds, this div and it's content will not be present on the dom. -->
</div>
<myComponent>
<!-- this component is loaded immediately without waiting for the library to load -->
<!-- you can't use somelibrary.js here -->
</myComponent>
please note that the first myComponent component will wait for library to load and will run after the second one, the error you see first is actually logged by the second component.
note: it adds the library to the bottom of your index.html, you can use the directive as much as you want, each url will be added once
import { Component, OnInit, Renderer2, Inject } from "@angular/core";
import { DOCUMENT } from "@angular/platform-browser";
@Component({
selector: "myComponent",
templateUrl: "./my-component.component.html",
styleUrls: ["./my-component.component.css"]
})
export class MyComponent implements OnInit {
constructor(
private renderer2: Renderer2,
@Inject(DOCUMENT) private _document
) {}
ngOnInit() {
const s = this.renderer2.createElement("script");
s.type = "text/javascript";
s.text = `somelibrary.doSomething()`; //this is where you use your library
this.renderer2.appendChild(this._document.body, s);
}
}
note: this is a very basic example, it is meant to showcase how you can use a script afer it is loaded, do not use it like this, this code will add the script YOU WROTE in this example (not the external js you are trying to load, that one will be added only once) a second time when user navigates to that page again. implement your own logic (hint: maybe use a service to keep track of the script tag with your code and check if it is already present on the page), make sure you understand what you are doing. my take is, using ids, bindings and maybe even ajax, my library should make your life a lot easier.
By default Angular can handle lazy loading modules.sometimes you have no choice but to use external js libraries in your code and you can't use scripts that don't have typescript types, unless you add them to scripts array. depending on the library they can be very large in size, and bundling them with angular will cause your app to become very heavy. the files you list as scripts for angular will be present even on your login page. why would you want to load a heavy pdf library, for example, when the user is trying to log in?
you can use angular-lazyscripts to add your script to the DOM, right before you use it in your component.this will improve loading time of your app.
this library was tested with angular-cli 7.3.8
and node 10
, I didn't use observables to avoid rxjs import problems. I think you should be fine in 6+ versions of angular, but if something starts acting up and you get errors, feel free to email me.
if you actually test it on lower versions and it works, please do inform me.
there is a 15 seconds timeout after which the component will not be added to the page. there won't be any errors. you are responsible with the file you choose.
I have included an archive of my test project. it loads big.js and tries to log 2 + 5 to console. the codes are commented. normally you should be fine with the documentation on this page.
Copyright (c) 2019 Antonio Ramirez sepehralizade@live.com, MIT License
FAQs
lazy load external js scripts and libraries in your angular projects. this module will handle loading the script you need before you use it in your components.
We found that angular-lazyscripts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.