SlideShare a Scribd company logo
Angular
Christoffer Noring
Google Developer Expert
Frontend Developer OVO Energy
@chris_noring
modules
Helps organize an application into cohesive blocks of functi
Decorates class with @NgModule
Angular libs that are modules
FormsModule, HttpModule, RouterModule
Third party libs
Material Design, AngularFire, Ionic
Can be lazy loaded
Can be eagerly loaded
angular.module(‘app’,[
])
Declare module
Declare constructs
belonging to said module
.controller()
.directive()
.value()
.provider()
Declare dependant modules‘dep1’,
‘dep2’
Angular 1
Service - singleton
We care about state
export class Service {
getData(){
}
}
@injectable()
Declare
import { Service } from ‘services/service’;
export class SomeComponent{
constructor(
private srv:Service,
private other:OtherService){
}
doStuff(){
var data = this.srv.getData();
}
Consume
Don’t forget
@injectable
keyword
Inject as usual
Use
@NgModule({
imports : [ BrowserModule ],
declarations : [
AppComponent,
SomeComponent,
SomePipe,
SomeDirective
]
bootstrap : [ AppComponent ],
providers : [ serviceForThisModule ],
entryComponents : []
})
export class AppModule{
}
A2 - Module anatomy
Dependant modules
Constructs the
module consist of
Entry point of the app
Injectable services
Angular Modules
CommonModule
ngIf
ngFor
imports and reexports
BrowserModule FormsModule
Directives and Components
supporting template driven forms
ngModel
Root Module
A module to launch the app
@NgModule({
imports : [
BrowserModule,
FeatureModule,
CommonModule
],
declarations : [
AppComponent,
SomeComponent,
SomePipe,
SomeDirective
]
bootstrap : [ AppComponent ],
providers : [ serviceForThisModule ]
})
export class AppModule{
}
All other modules
your app consist of
is listed in the imports :[]
The entry component
is pointed out
in the bootstrap : []
Bootstrapping
With JIT
With AOT
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModuleNgFactory } from ‘./app.module.ngfactory';
platformBrowserDynamic().bootstrapModule(AppModuleNgFactory);
platformBrowserDynamic().bootstrapModule(AppModule);
Root module
Feature Module
@NgModule({
imports : [
FormsModule,
CommonModule
],
declarations : [
FeatureComponent,
SomeOtherComponent,
FeaturePipe,
FeatureDirective
]
exports : [ CommonComponent ],
providers : [ serviceForThisModule ]
})
export class FeatureModule{
}
Dependant modules
Component/constructs
the module consists of
Publicly usable constructs
A module to extend the app
Import
Allows us to use constructs from other modules such as
Directives
Pipes
Components
But only that which is public
imports : [ SomeModule ],
exports : [
FeatureComponent
]
FeatureModuleSomeModule
imports : [ ],
exports : [
SomeComponent,
SomeDirective
] Can be used in
FeatureComponent markup
Exporting
A normal feature module would only export the top component
ProductDetailComponentProductAdvertisementComponent
This would not be exported
as they are helper components
ProductListComponent This would be exported
exports : [
ProductListComponent,
]
Re export
Reexport is making constructs from other modules available
imports : [ ],
exports : [
SomeModule,
FeatureComponent
]
FeatureModule
make public SomeModule constructs available
for importers of FeatureModule
Example from angular BrowserModule reexports CommonModule, App
SomeModule
imports : [ ],
exports : [
SomeComponent,
SomeDirective
]
public
ParentModule
imports
When not to re-export
Module
Service1 Service2 Service3
is no construct such as directive, pipes or components but only s
example HttpModule
SharedModule
what to export
HelperComponent
CommonComponent CommonDirective
MoneyPipe FilteringPipe
Most constructs would likely be exported
exports : [
CommonComponent,
CommonDirective,
MoneyPipe,
FilteringPipe
]
Majority is exported, minority not exported
App architecture
AppModule
CoreModuleContactsModule ProductsModule CommonModule
Feature modules
FormsModule BrowserModule RouterModule
Angular core modules
Core Module
For application wide services,
places them in a core module
This ensures they are singletons
@NgModule({
imports : [
…
],
declarations : [
CoreComponent..
]
exports : [ CommonComponent ],
providers : [
applicationWideService,
applicationWideService2
]
})
export class CoreModule{
CoreModule
Services
Import only from AppModule
@NgModule({
imports : [
CoreModule
],
declarations : [
AppComponent
]
bootstrap : [ AppComponent ],
providers : [
]
})
export class AppModule{
}
AppModule
Providers
Application scoped by design
A certain service can be injected
in any construct
Providers listed in @NgModule.providers have application scope
When a module is imported the modules providers list is added root injec
@NgModule({
imports : [
SomeModule
],
declarations : [
SomeComponent
]
exports : [ SomeComponent ],
providers : [
service1, service2, service3
]
})
export class SomeModule{
}
root
injector
Word of caution
Import modules with providers only once
Provide the service in a component if component needs its
own copy, but generally add it to the module’s providers
Try to lazy load modules as much as possible,
they get their own injector
aka providers overridden by mistake
App-wide providers should be added to AppModule
not AppComponent if you have lazy loaded modules
Import HttpModule only in AppModule to avoid provider corruption
SomeModule
HttpModule
OtherModule
HttpModule
providers providers
configuration done
in one provider might be lost
Lazy load
app.routing.ts
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full',
{ path: 'about', loadChildren: './+about/about.module#AboutModule' }
];
#AboutModule Name of module
+ Naming convention of lazy loaded modules
./+about/about.routing.ts
const routes: Routes = [
{ path: '', component: AboutComponent },
];
File Module class
Don’t place services in a
shared module,
there would be one instance
per lazy loaded module
different
instances
Feature
Module
Shared
Module
lazy
loaded
Service1:instance1
imports
Feature
Module
Shared
Module
lazy
loaded
Service1:instance2
imports
constructor(service1:Service1) {
}
Shared
Module
Service1 Service2 Service3
Prevent reimport of core
module
CoreModule
constructor (@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error(
'CoreModule is already loaded. Import it in the AppModule only');
}
}
Throw error if someone
but root module imports me
Don’t let my own injector instantiate me
AppModule
CoreModule
imports
FeatureModule
CoreModule
imports
Refactor
Lets take an existing app and divide in suitable modules
Looks like feature component
Looks like feature service
Looks like an application wide service
Lets refactor
Looks like a reusable construct
@NgModule({
imports : [
BrowserModule
],
declarations : [
AppComponent,
AboutComponent,
MoneyPipe
]
bootstrap : [ AppComponent ],
providers : [
AboutService,
AuthService
]
})
AppModule
@NgModule({
imports : [
AboutModule,
CoreModule
],
declarations : [
AppComponent,
]
bootstrap : [ AppComponent ],
providers : [
]
})
AppModule
@NgModule({
imports : [
SharedModule
],
declarations : [
AboutComponent,
]
bootstrap : [ AppComponent ],
providers : [
AboutService
]
})
AboutModule
AboutModule (Feature)
AboutComponent
AboutService
1
SharedModule
MoneyPipe2
CoreModule
AuthService3
4
5
imports BrowserModule 5
Summary
Components, Directives and Pipes
are module scoped
Unless exported with exports keyword
Services listed in module providers is application wide
Divide up your app in suitable modules
Only have application wide services in a core module
that is imported by the AppModule
Have shared constructs in shared modules but don't have providers in there on mo
Modules can make its constructs public by exposing it under exports keyword
Only the root module has the bootstrap keyword
Thank you
@chris_noring

More Related Content

PPTX
Angular 9
Raja Vishnu
 
PPTX
Angular Data Binding
Jennifer Estrada
 
PPTX
Angular 5 presentation for beginners
Imran Qasim
 
PPT
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
DOCX
Angular Interview Questions & Answers
Ratnala Charan kumar
 
PDF
Angular material
Kalpesh Satasiya
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
ODP
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
Angular 9
Raja Vishnu
 
Angular Data Binding
Jennifer Estrada
 
Angular 5 presentation for beginners
Imran Qasim
 
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
Angular Interview Questions & Answers
Ratnala Charan kumar
 
Angular material
Kalpesh Satasiya
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 

What's hot (20)

PPTX
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
PDF
Angular Advanced Routing
Laurent Duveau
 
PPTX
Angular introduction students
Christian John Felix
 
PPTX
Introduction to angular with a simple but complete project
Jadson Santos
 
PPT
Angular 8
Sunil OS
 
PDF
Building blocks of Angular
Knoldus Inc.
 
PDF
Angular
Lilia Sfaxi
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PPT
Angular App Presentation
Elizabeth Long
 
PDF
Angular components
Sultan Ahmed
 
PDF
Angular routing
Sultan Ahmed
 
PDF
Angular 16 – the rise of Signals
Coding Academy
 
PPTX
Angular tutorial
Rohit Gupta
 
PDF
Angular data binding
Sultan Ahmed
 
PPTX
Introduction to Spring Boot
Purbarun Chakrabarti
 
PPTX
Modules in AngularJs
K Arunkumar
 
PPTX
Angular interview questions
Goa App
 
PDF
Angular Directives
iFour Technolab Pvt. Ltd.
 
PPTX
Angular 14.pptx
MohaNedGhawar
 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
 
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
Angular Advanced Routing
Laurent Duveau
 
Angular introduction students
Christian John Felix
 
Introduction to angular with a simple but complete project
Jadson Santos
 
Angular 8
Sunil OS
 
Building blocks of Angular
Knoldus Inc.
 
Angular
Lilia Sfaxi
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular App Presentation
Elizabeth Long
 
Angular components
Sultan Ahmed
 
Angular routing
Sultan Ahmed
 
Angular 16 – the rise of Signals
Coding Academy
 
Angular tutorial
Rohit Gupta
 
Angular data binding
Sultan Ahmed
 
Introduction to Spring Boot
Purbarun Chakrabarti
 
Modules in AngularJs
K Arunkumar
 
Angular interview questions
Goa App
 
Angular Directives
iFour Technolab Pvt. Ltd.
 
Angular 14.pptx
MohaNedGhawar
 
Angular - Chapter 5 - Directives
WebStackAcademy
 
Ad

Viewers also liked (14)

PPTX
Rxjs ppt
Christoffer Noring
 
PPTX
Rxjs swetugg
Christoffer Noring
 
PPTX
Rxjs ngvikings
Christoffer Noring
 
PPTX
Performance Optimization In Angular 2
Eyal Vardi
 
PDF
React lecture
Christoffer Noring
 
PPTX
Angular Workshop_Sarajevo2
Christoffer Noring
 
PPTX
Typescript barcelona
Christoffer Noring
 
PPTX
Finjs - Angular 2 better faster stronger
Christoffer Noring
 
PPTX
Firebase ng2 zurich
Christoffer Noring
 
PPTX
Angular2 rxjs
Christoffer Noring
 
PPTX
Angular2 + rxjs
Christoffer Noring
 
PPTX
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
PDF
Introduction to Angular 2
Dawid Myslak
 
PPTX
Nativescript with angular 2
Christoffer Noring
 
Rxjs swetugg
Christoffer Noring
 
Rxjs ngvikings
Christoffer Noring
 
Performance Optimization In Angular 2
Eyal Vardi
 
React lecture
Christoffer Noring
 
Angular Workshop_Sarajevo2
Christoffer Noring
 
Typescript barcelona
Christoffer Noring
 
Finjs - Angular 2 better faster stronger
Christoffer Noring
 
Firebase ng2 zurich
Christoffer Noring
 
Angular2 rxjs
Christoffer Noring
 
Angular2 + rxjs
Christoffer Noring
 
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
Introduction to Angular 2
Dawid Myslak
 
Nativescript with angular 2
Christoffer Noring
 
Ad

Similar to Angular modules in depth (20)

PPTX
mobile development using node js and java
vishal choudhary
 
PDF
Angular Interview Question & Answers PDF By ScholarHat
Scholarhat
 
PDF
Angular resolver tutorial
Katy Slemon
 
PPT
17612235.ppt
yovixi5669
 
PDF
better-apps-angular-2-day1.pdf and home
ChethanGowda886434
 
PDF
Dependency Injection pattern in Angular
Alexe Bogdan
 
PPTX
Angular crash course
Birhan Nega
 
PPTX
Angularj2.0
Mallikarjuna G D
 
PDF
What is your money doing?
Alfonso Fernández
 
PDF
Building Blocks of Angular 2 and ASP.NET Core
Levi Fuller
 
PPTX
Angularjs2 presentation
dharisk
 
PDF
A Story about AngularJS modularization development
Johannes Weber
 
PDF
Angular2 with type script
Ravi Mone
 
PDF
Angular 4 for Java Developers
Yakov Fain
 
PDF
AngularJS - Services
Nir Kaufman
 
PDF
Angular2
SitaPrajapati
 
PDF
Angular_BootStrap.pdf
raja344116
 
PDF
"Angular.js Concepts in Depth" by Aleksandar Simović
JS Belgrade
 
mobile development using node js and java
vishal choudhary
 
Angular Interview Question & Answers PDF By ScholarHat
Scholarhat
 
Angular resolver tutorial
Katy Slemon
 
17612235.ppt
yovixi5669
 
better-apps-angular-2-day1.pdf and home
ChethanGowda886434
 
Dependency Injection pattern in Angular
Alexe Bogdan
 
Angular crash course
Birhan Nega
 
Angularj2.0
Mallikarjuna G D
 
What is your money doing?
Alfonso Fernández
 
Building Blocks of Angular 2 and ASP.NET Core
Levi Fuller
 
Angularjs2 presentation
dharisk
 
A Story about AngularJS modularization development
Johannes Weber
 
Angular2 with type script
Ravi Mone
 
Angular 4 for Java Developers
Yakov Fain
 
AngularJS - Services
Nir Kaufman
 
Angular2
SitaPrajapati
 
Angular_BootStrap.pdf
raja344116
 
"Angular.js Concepts in Depth" by Aleksandar Simović
JS Belgrade
 

More from Christoffer Noring (20)

PPTX
Azure signalR
Christoffer Noring
 
PPTX
Game dev 101 part 3
Christoffer Noring
 
PPTX
Game dev 101 part 2
Christoffer Noring
 
PPTX
Game dev workshop
Christoffer Noring
 
PPTX
Deploying your static web app to the Cloud
Christoffer Noring
 
PPTX
IaaS with ARM templates for Azure
Christoffer Noring
 
PPTX
Learning Svelte
Christoffer Noring
 
PPTX
Ng spain
Christoffer Noring
 
PDF
Angular Schematics
Christoffer Noring
 
PDF
Design thinking
Christoffer Noring
 
PDF
Keynote ijs
Christoffer Noring
 
PDF
Vue fundamentasl with Testing and Vuex
Christoffer Noring
 
PDF
Ngrx slides
Christoffer Noring
 
PDF
Kendoui
Christoffer Noring
 
PPTX
Angular mix chrisnoring
Christoffer Noring
 
PDF
Nativescript angular
Christoffer Noring
 
PDF
Graphql, REST and Apollo
Christoffer Noring
 
PDF
Angular 2 introduction
Christoffer Noring
 
PDF
Rxjs vienna
Christoffer Noring
 
PPTX
Rxjs marble-testing
Christoffer Noring
 
Azure signalR
Christoffer Noring
 
Game dev 101 part 3
Christoffer Noring
 
Game dev 101 part 2
Christoffer Noring
 
Game dev workshop
Christoffer Noring
 
Deploying your static web app to the Cloud
Christoffer Noring
 
IaaS with ARM templates for Azure
Christoffer Noring
 
Learning Svelte
Christoffer Noring
 
Angular Schematics
Christoffer Noring
 
Design thinking
Christoffer Noring
 
Keynote ijs
Christoffer Noring
 
Vue fundamentasl with Testing and Vuex
Christoffer Noring
 
Ngrx slides
Christoffer Noring
 
Angular mix chrisnoring
Christoffer Noring
 
Nativescript angular
Christoffer Noring
 
Graphql, REST and Apollo
Christoffer Noring
 
Angular 2 introduction
Christoffer Noring
 
Rxjs vienna
Christoffer Noring
 
Rxjs marble-testing
Christoffer Noring
 

Recently uploaded (20)

PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
This slide provides an overview Technology
mineshkharadi333
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 

Angular modules in depth

  • 1. Angular Christoffer Noring Google Developer Expert Frontend Developer OVO Energy @chris_noring modules
  • 2. Helps organize an application into cohesive blocks of functi Decorates class with @NgModule Angular libs that are modules FormsModule, HttpModule, RouterModule Third party libs Material Design, AngularFire, Ionic Can be lazy loaded Can be eagerly loaded
  • 3. angular.module(‘app’,[ ]) Declare module Declare constructs belonging to said module .controller() .directive() .value() .provider() Declare dependant modules‘dep1’, ‘dep2’ Angular 1
  • 4. Service - singleton We care about state export class Service { getData(){ } } @injectable() Declare import { Service } from ‘services/service’; export class SomeComponent{ constructor( private srv:Service, private other:OtherService){ } doStuff(){ var data = this.srv.getData(); } Consume Don’t forget @injectable keyword Inject as usual Use
  • 5. @NgModule({ imports : [ BrowserModule ], declarations : [ AppComponent, SomeComponent, SomePipe, SomeDirective ] bootstrap : [ AppComponent ], providers : [ serviceForThisModule ], entryComponents : [] }) export class AppModule{ } A2 - Module anatomy Dependant modules Constructs the module consist of Entry point of the app Injectable services
  • 6. Angular Modules CommonModule ngIf ngFor imports and reexports BrowserModule FormsModule Directives and Components supporting template driven forms ngModel
  • 7. Root Module A module to launch the app @NgModule({ imports : [ BrowserModule, FeatureModule, CommonModule ], declarations : [ AppComponent, SomeComponent, SomePipe, SomeDirective ] bootstrap : [ AppComponent ], providers : [ serviceForThisModule ] }) export class AppModule{ } All other modules your app consist of is listed in the imports :[] The entry component is pointed out in the bootstrap : []
  • 8. Bootstrapping With JIT With AOT import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModuleNgFactory } from ‘./app.module.ngfactory'; platformBrowserDynamic().bootstrapModule(AppModuleNgFactory); platformBrowserDynamic().bootstrapModule(AppModule); Root module
  • 9. Feature Module @NgModule({ imports : [ FormsModule, CommonModule ], declarations : [ FeatureComponent, SomeOtherComponent, FeaturePipe, FeatureDirective ] exports : [ CommonComponent ], providers : [ serviceForThisModule ] }) export class FeatureModule{ } Dependant modules Component/constructs the module consists of Publicly usable constructs A module to extend the app
  • 10. Import Allows us to use constructs from other modules such as Directives Pipes Components But only that which is public imports : [ SomeModule ], exports : [ FeatureComponent ] FeatureModuleSomeModule imports : [ ], exports : [ SomeComponent, SomeDirective ] Can be used in FeatureComponent markup
  • 11. Exporting A normal feature module would only export the top component ProductDetailComponentProductAdvertisementComponent This would not be exported as they are helper components ProductListComponent This would be exported exports : [ ProductListComponent, ]
  • 12. Re export Reexport is making constructs from other modules available imports : [ ], exports : [ SomeModule, FeatureComponent ] FeatureModule make public SomeModule constructs available for importers of FeatureModule Example from angular BrowserModule reexports CommonModule, App SomeModule imports : [ ], exports : [ SomeComponent, SomeDirective ] public ParentModule imports
  • 13. When not to re-export Module Service1 Service2 Service3 is no construct such as directive, pipes or components but only s example HttpModule
  • 14. SharedModule what to export HelperComponent CommonComponent CommonDirective MoneyPipe FilteringPipe Most constructs would likely be exported exports : [ CommonComponent, CommonDirective, MoneyPipe, FilteringPipe ] Majority is exported, minority not exported
  • 15. App architecture AppModule CoreModuleContactsModule ProductsModule CommonModule Feature modules FormsModule BrowserModule RouterModule Angular core modules
  • 16. Core Module For application wide services, places them in a core module This ensures they are singletons @NgModule({ imports : [ … ], declarations : [ CoreComponent.. ] exports : [ CommonComponent ], providers : [ applicationWideService, applicationWideService2 ] }) export class CoreModule{ CoreModule Services Import only from AppModule @NgModule({ imports : [ CoreModule ], declarations : [ AppComponent ] bootstrap : [ AppComponent ], providers : [ ] }) export class AppModule{ } AppModule
  • 17. Providers Application scoped by design A certain service can be injected in any construct Providers listed in @NgModule.providers have application scope When a module is imported the modules providers list is added root injec @NgModule({ imports : [ SomeModule ], declarations : [ SomeComponent ] exports : [ SomeComponent ], providers : [ service1, service2, service3 ] }) export class SomeModule{ } root injector
  • 18. Word of caution Import modules with providers only once Provide the service in a component if component needs its own copy, but generally add it to the module’s providers Try to lazy load modules as much as possible, they get their own injector aka providers overridden by mistake App-wide providers should be added to AppModule not AppComponent if you have lazy loaded modules Import HttpModule only in AppModule to avoid provider corruption SomeModule HttpModule OtherModule HttpModule providers providers configuration done in one provider might be lost
  • 19. Lazy load app.routing.ts const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full', { path: 'about', loadChildren: './+about/about.module#AboutModule' } ]; #AboutModule Name of module + Naming convention of lazy loaded modules ./+about/about.routing.ts const routes: Routes = [ { path: '', component: AboutComponent }, ]; File Module class
  • 20. Don’t place services in a shared module, there would be one instance per lazy loaded module different instances Feature Module Shared Module lazy loaded Service1:instance1 imports Feature Module Shared Module lazy loaded Service1:instance2 imports constructor(service1:Service1) { } Shared Module Service1 Service2 Service3
  • 21. Prevent reimport of core module CoreModule constructor (@Optional() @SkipSelf() parentModule: CoreModule) { if (parentModule) { throw new Error( 'CoreModule is already loaded. Import it in the AppModule only'); } } Throw error if someone but root module imports me Don’t let my own injector instantiate me AppModule CoreModule imports FeatureModule CoreModule imports
  • 22. Refactor Lets take an existing app and divide in suitable modules Looks like feature component Looks like feature service Looks like an application wide service Lets refactor Looks like a reusable construct @NgModule({ imports : [ BrowserModule ], declarations : [ AppComponent, AboutComponent, MoneyPipe ] bootstrap : [ AppComponent ], providers : [ AboutService, AuthService ] }) AppModule
  • 23. @NgModule({ imports : [ AboutModule, CoreModule ], declarations : [ AppComponent, ] bootstrap : [ AppComponent ], providers : [ ] }) AppModule @NgModule({ imports : [ SharedModule ], declarations : [ AboutComponent, ] bootstrap : [ AppComponent ], providers : [ AboutService ] }) AboutModule AboutModule (Feature) AboutComponent AboutService 1 SharedModule MoneyPipe2 CoreModule AuthService3 4 5 imports BrowserModule 5
  • 24. Summary Components, Directives and Pipes are module scoped Unless exported with exports keyword Services listed in module providers is application wide Divide up your app in suitable modules Only have application wide services in a core module that is imported by the AppModule Have shared constructs in shared modules but don't have providers in there on mo Modules can make its constructs public by exposing it under exports keyword Only the root module has the bootstrap keyword