Dany Paredes
Dany Paredes | Javascript / Web

Dany Paredes | Javascript / Web

How to control global objects in Angular.

How to control global objects in Angular.

Dany Paredes's photo
Dany Paredes
·Oct 5, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Install leaflet
  • Leaflet API
  • Use the InjectionToken Class
  • Provide the Leaflet
  • Using @Inject
  • That's it!

When we use external libraries, it is very common to declare a global object and use it. But the price to pay is get complex testing scenario, and of course global object like magic is not a “good practice”.

How I can tell to angular about provide an external library declared as global?

My example was using the leaflet library, using the InjectionToken class and @Inject.

If you want to read more about it.


Install leaflet

Install the leaflet package and register into the angular.json to load the library.

npm install leaflet

Open the angular.json file and add leaflet.css and leaflet.js assets.

     "styles": [
     "scripts": [
          "configurations": { ...

Leaflet API

To use the methods provide by leaflet, we define the contract with the global object. It's optional, but makes our code easy to follow, so create an interface with the public methods.

export interface LeafletAPI { 
   setView(points: [], id:number): object;
   tileLayer(url:string, options:object): object;

Use the InjectionToken Class

Import the InjectionToken class from @angular/core, it helps us create new instance, given the LeafletAPI. And find the global object using a string name. The leaflet value is “L”.

import { InjectionToken} from '@angular/core';
export let LEAFLET_TOKEN = new InjectionToken<LeafletAPI>('L');

Provide the Leaflet

In the AppModule, declare a variable for the L, register the LEAFLET_TOKEN and set the useValue to L, into the providers.

Now, Angular return an instance of L when, someone when request the LEAFLET_TOKEN to be injected.

import { NgModule } from  '@angular/core';
import { BrowserModule } from  '@angular/platform-browser';
import { AppComponent } from  './app.component';
import { LealefAPI, LEALEF_TOKEN } from  './services/lealef.injector';
declare  let  L:  LealefAPI;

    declarations: [
    imports: [BrowserModule],
    providers: [
        { provide: LEALEF_TOKEN, useValue: L}
    bootstrap: [AppComponent]
export  class  AppModule { }

Using @Inject

The @Inject() allow us to let Angular know which object must be injected, so using the token, the DI will return the value declared in the providers for our token.

In our case the key is the LEAFLET_TOKEN, angular load it from our register provider and create a new service MapService, in the constructor use declare leaflet field using @Inject and the token.

import { Inject, Injectable } from '@angular/core';
import { LeafletAPI, LEAFLET_TOKEN } from './lealef.injector';

export class MapService {
    constructor(@Inject(LEAFLET_TOKEN) private _leaflet: LealefAPI) { }

The Leaflet was injected on the MapService by the Angular dependency injector, and we are ready to use the methods provided by LealefAPI.

export class MapService {
   constructor(@Inject(LEAFLET_TOKEN) private _leaflet: LealefAPI) { }

   showMenorca(): void {
        let map = this._leaflef.map('mapid').setView([39.9255, 4.032], 13);
        const tiles = this._leaflef.tileLayer(
                maxZoom: 8,
                minZoom: 3

That's it!

Hopefully, that will give you a bit of help with how avoid global object and use InjectionToken and @Inject. If you enjoyed this post, share it!

Photo by Fernando @cferdo on Unsplash

Did you find this article valuable?

Support Dany Paredes by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this