SlideShare a Scribd company logo
Injection Tokens &
Custom Libraries
About mySelf
• Experienced FE developer, specialised in B2C applications
• FE trainer & lecturer @ 500Tech
• Weekends FE developer @ fashbash.co
Let’s build a strong Angular
community together!!
Angular - injection tokens & Custom libraries
Angular - injection tokens & Custom libraries
Talk Objective - logger package
● Who can be installed easily by NPM
● Should be reusable across many applications
● Who can log any http request from our http client
● Each application should have different log prefix
● Safe to use at compile time
Talk Objective - logger package
Talk Objective - logger package
DI
Angular libs
Injection Tokens
Module With Providers
Libraries
The pack your code into a NPM
package, and reuse it within other
projects
Angular - injection tokens & Custom libraries
@NgModule({
declarations: [...],
imports: [
AngularFireModule.initializeApp({
apiKey: “<YOUR API KEY>",
authDomain: “<AUTH DOMAIN>",
databaseURL: “<DB URL>",
projectId: “<PROJECT ID>",
storageBucket: “<STORAGE BUCKET>",
messagingSenderId: “<MESSAGING ID>"
}),
],
providers: [...],
bootstrap: [AppComponent]
})
export class AppModule { }
ng-packger
● Library generator
● Configure for testing and building
the Module out of the box
● Started as an open source

project, now -> Angular CLI
DI
Why we need Dependency Injection?
Separate the concept of depending
on something from the knowledge of
how to construct it
`
Decoupling Reuse Testing
Why we need Dependency Injection?
What can’t be injected?
● Primitives - strings, numbers etc…
● Interfaces - objects
● Functions
● Observables
Tokens
● Key Which identifies a value to injected
● Not have to be declared at build time, Must exist at
runtime
InjectionToken<T>
● The key to allow us to inject the reference of an
arbitrated type
● In simple words - a placeholder to be override, and can
be injected everywhere you want!
Create our own Injection Token
export const loggerConfig = new InjectionToken<LoggerConfig>('Config for
logger interceptor');
constructor(@Inject(loggerConfig) private config: LoggerConfig) { }
How we can provide the run time
value to the Injection Token?
Providers!
Provide the value
useClass
@NgModule({
declarations: [],
imports: [
],
providers: [ {provide: LoggerService, useClass: LoggerService}],
exports: [LoggerComponent]
})
Provide the value
useClass - syntactic sugar
@NgModule({
declarations: [],
imports: [
],
providers: [LoggerService],
exports: [LoggerComponent]
})
Provide the value
useValue
@NgModule({
declarations: [],
imports: [
],
providers: [{ provide: loggerConfig, useValue: {appName: 'APP1'} }],
exports: [LoggerComponent]
})
Provide the value
useExisting
@NgModule({
declarations: [],
imports: [
],
providers: [{ provide: LoggerService, useExisting: HttpLoggerService }],
exports: [LoggerComponent]
})
Provide the value
useFactory
@NgModule({
declarations: [],
imports: [
],
providers: [{ provide: LoggerService, useFactory: () => {
return new LoggerService();
} }],
exports: [LoggerComponent]
})
Multi providers
● We have the ability to provide
multi providers into the same
Token - 

HTTP_INTERCEPTOR,
APP_INITIZALIER
@NgModule({
bootstrap: [AppComponent],
imports: [...],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: LoggerService,
multi: true
}
]
})
Deps (dependencies)
● Sometimes providers
depends on other
providers to get
instantiated.
@NgModule({
declarations: [],
imports: [
],
providers: [
{
provide: APP_INITIALIZER,
useFactory: init_app,
deps: [AppInitService],
multi: true
},
],
exports: [LoggerComponent]
})
@Optional
● DI climbing up the tree and looking for the instance
of the token we Injected
● If no instance provided -> No provider for exception
constructor(@Optional() @Inject(loggerConfig)
private config: LoggerConfig) { }
Now…we know how to provide the
value to our tokens…
How the end-user should 

know this tokens exists?
BUT
ModuleWithProviders
● Static function inside the module that can get
parameters
● Return ngModule but controlling the providers
ModuleWithProviders - Angular Router
export class RouterModule {
static forRoot(routes: Routes, config?: ExtraOptions):
ModuleWithProviders<RouterModule> {
return {
ngModule: RouterModule,
providers: [
...
],
};
}
}
Test, build and deploy you lib
ng build *lib-name*
ng test *lib-name*
cd dist/*lib-name*
npm version major/minor/patch
npm login
npm publish
Few more things…
UI component Module
NX
Thank You
@EliranEliassy eliran.eliassy@gmail.comeliraneliassy

More Related Content

PDF
Angular - Improve Runtime performance 2019
PDF
Ngrx meta reducers
PDF
Angular server side rendering - Strategies & Technics
PDF
Angular genericforms2
PPTX
Angular js 2
ODP
Jquery- One slide completing all JQuery
PDF
Exploring Angular 2 - Episode 2
PPTX
AngularJS (1.x) as fast as a lightning
Angular - Improve Runtime performance 2019
Ngrx meta reducers
Angular server side rendering - Strategies & Technics
Angular genericforms2
Angular js 2
Jquery- One slide completing all JQuery
Exploring Angular 2 - Episode 2
AngularJS (1.x) as fast as a lightning

What's hot (20)

ODP
Unit Testing and Coverage for AngularJS
PDF
Reactive Programming with JavaScript
PPTX
Firebase ng2 zurich
PPTX
Angular 2.0 Dependency injection
PDF
Angular2 - In Action
PDF
The Road to Native Web Components
PDF
Angular 2.0 - What to expect
PPTX
Async patterns in javascript
PDF
Angular 2: core concepts
PPTX
AngularJS Unit Testing
PDF
Angular2 workshop
PDF
Angular Weekend
PDF
An introduction to Angular2
PPTX
AngularJs presentation
PDF
Angular Dependency Injection
PDF
ngMess: AngularJS Dependency Injection
PDF
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
ODP
Angular JS Unit Testing - Overview
PPTX
Angular 2 NgModule
PPTX
AngularJs
Unit Testing and Coverage for AngularJS
Reactive Programming with JavaScript
Firebase ng2 zurich
Angular 2.0 Dependency injection
Angular2 - In Action
The Road to Native Web Components
Angular 2.0 - What to expect
Async patterns in javascript
Angular 2: core concepts
AngularJS Unit Testing
Angular2 workshop
Angular Weekend
An introduction to Angular2
AngularJs presentation
Angular Dependency Injection
ngMess: AngularJS Dependency Injection
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Angular JS Unit Testing - Overview
Angular 2 NgModule
AngularJs
Ad

Similar to Angular - injection tokens & Custom libraries (20)

PDF
Introduction to AngularJS
PDF
Vitaliy Makogon: Migration to ivy. Angular component libraries with IVY support.
PDF
Angular for Java Enterprise Developers: Oracle Code One 2018
PPTX
Front end development with Angular JS
PDF
Leveraging Playwright for API Testing.pdf
PDF
AngularJS - Services
PDF
S313937 cdi dochez
PDF
服务框架: Thrift & PasteScript
PDF
How to create an Angular builder
PDF
Scala, docker and testing, oh my! mario camou
PDF
Meetup SkillValue - Angular 6 : Bien démarrer son application
PDF
better-apps-angular-2-day1.pdf and home
KEY
Building production-quality apps with Node.js
PDF
Introduction to Angular 2
PPTX
Use Eclipse technologies to build a modern embedded IDE
PDF
How to build a Whatsapp clone in 2 hours
PDF
How to build a Whatsapp clone in 2 hours
PDF
Angular 2 for Java Developers
PDF
Build Great Networked APIs with Swift, OpenAPI, and gRPC
PPTX
angular-concepts-introduction-slides.pptx
Introduction to AngularJS
Vitaliy Makogon: Migration to ivy. Angular component libraries with IVY support.
Angular for Java Enterprise Developers: Oracle Code One 2018
Front end development with Angular JS
Leveraging Playwright for API Testing.pdf
AngularJS - Services
S313937 cdi dochez
服务框架: Thrift & PasteScript
How to create an Angular builder
Scala, docker and testing, oh my! mario camou
Meetup SkillValue - Angular 6 : Bien démarrer son application
better-apps-angular-2-day1.pdf and home
Building production-quality apps with Node.js
Introduction to Angular 2
Use Eclipse technologies to build a modern embedded IDE
How to build a Whatsapp clone in 2 hours
How to build a Whatsapp clone in 2 hours
Angular 2 for Java Developers
Build Great Networked APIs with Swift, OpenAPI, and gRPC
angular-concepts-introduction-slides.pptx
Ad

More from Eliran Eliassy (6)

PDF
Between JS and AI
PDF
Angular CDK
PDF
Runtime performance
PDF
Intro to HTML and CSS basics
PDF
Angular performance improvments
PPTX
Generic forms
Between JS and AI
Angular CDK
Runtime performance
Intro to HTML and CSS basics
Angular performance improvments
Generic forms

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Spectroscopy.pptx food analysis technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectroscopy.pptx food analysis technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Digital-Transformation-Roadmap-for-Companies.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Assigned Numbers - 2025 - Bluetooth® Document
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology

Angular - injection tokens & Custom libraries