SlideShare ist ein Scribd-Unternehmen logo
1 von 25
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

Weitere ähnliche Inhalte

Was ist angesagt?

Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced RoutingLaurent Duveau
 
Angular 8
Angular 8 Angular 8
Angular 8 Sunil OS
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular ComponentsSquash Apps Pvt Ltd
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginnersImran Qasim
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency InjectionNir Kaufman
 
Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes WorkshopNir Kaufman
 
Angular directives and pipes
Angular directives and pipesAngular directives and pipes
Angular directives and pipesKnoldus Inc.
 
RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019Fabio Biondi
 
Introduzione ad angular 7/8
Introduzione ad angular 7/8Introduzione ad angular 7/8
Introduzione ad angular 7/8Valerio Radice
 
Angular 10 course_content
Angular 10 course_contentAngular 10 course_content
Angular 10 course_contentNAVEENSAGGAM1
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - IntroductionWebStackAcademy
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of SignalsCoding Academy
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Edureka!
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorialRohit Gupta
 

Was ist angesagt? (20)

Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced Routing
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginners
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes Workshop
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Angular directives and pipes
Angular directives and pipesAngular directives and pipes
Angular directives and pipes
 
RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019
 
Introduzione ad angular 7/8
Introduzione ad angular 7/8Introduzione ad angular 7/8
Introduzione ad angular 7/8
 
Angular 10 course_content
Angular 10 course_contentAngular 10 course_content
Angular 10 course_content
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
Angular overview
Angular overviewAngular overview
Angular overview
 
Angular
AngularAngular
Angular
 
Angular routing
Angular routingAngular routing
Angular routing
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
 
AngularJS
AngularJSAngularJS
AngularJS
 
Angular Basics.pptx
Angular Basics.pptxAngular Basics.pptx
Angular Basics.pptx
 

Andere mochten auch (14)

Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
 
Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 
React lecture
React lectureReact lecture
React lecture
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
 
Finjs - Angular 2 better faster stronger
Finjs - Angular 2 better faster strongerFinjs - Angular 2 better faster stronger
Finjs - Angular 2 better faster stronger
 
Firebase ng2 zurich
Firebase ng2 zurichFirebase ng2 zurich
Firebase ng2 zurich
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Angular2 + rxjs
Angular2 + rxjsAngular2 + rxjs
Angular2 + rxjs
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
Nativescript with angular 2
Nativescript with angular 2Nativescript with angular 2
Nativescript with angular 2
 

Ähnlich wie Angular modules in depth

Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationThibault Even
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in AngularAlexe Bogdan
 
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2Ivan Matiishyn
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JSBipin
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slidesDavid Barreto
 
Angular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamAngular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamShubham Verma
 
Introduction to Zend Framework 2
Introduction to Zend Framework 2Introduction to Zend Framework 2
Introduction to Zend Framework 2John Coggeshall
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next FrameworkCommit University
 
Building a website with angular 2
Building a website with angular 2Building a website with angular 2
Building a website with angular 2Joseph Jorden
 
Building a website with angular
Building a website with angularBuilding a website with angular
Building a website with angularJoseph Jorden
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Frost
 
ngNewRouter
ngNewRouterngNewRouter
ngNewRouterphidong
 
Angular Course.pptx
Angular Course.pptxAngular Course.pptx
Angular Course.pptxImdad Ullah
 

Ähnlich wie Angular modules in depth (20)

Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son application
 
What is your money doing?
What is your money doing?What is your money doing?
What is your money doing?
 
Angular 2 in-1
Angular 2 in-1 Angular 2 in-1
Angular 2 in-1
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in Angular
 
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slides
 
Angular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamAngular2RoutingSetupByShubham
Angular2RoutingSetupByShubham
 
Introduction to Zend Framework 2
Introduction to Zend Framework 2Introduction to Zend Framework 2
Introduction to Zend Framework 2
 
Angular 2 Básico
Angular 2 BásicoAngular 2 Básico
Angular 2 Básico
 
17612235.ppt
17612235.ppt17612235.ppt
17612235.ppt
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next Framework
 
Angular2
Angular2Angular2
Angular2
 
Meteor
MeteorMeteor
Meteor
 
Building a website with angular 2
Building a website with angular 2Building a website with angular 2
Building a website with angular 2
 
Building a website with angular
Building a website with angularBuilding a website with angular
Building a website with angular
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
 
Di code steps
Di code stepsDi code steps
Di code steps
 
ngNewRouter
ngNewRouterngNewRouter
ngNewRouter
 
Angular Course.pptx
Angular Course.pptxAngular Course.pptx
Angular Course.pptx
 

Mehr von Christoffer Noring (20)

Azure signalR
Azure signalRAzure signalR
Azure signalR
 
Game dev 101 part 3
Game dev 101 part 3Game dev 101 part 3
Game dev 101 part 3
 
Game dev 101 part 2
Game dev 101   part 2Game dev 101   part 2
Game dev 101 part 2
 
Game dev workshop
Game dev workshopGame dev workshop
Game dev workshop
 
Deploying your static web app to the Cloud
Deploying your static web app to the CloudDeploying your static web app to the Cloud
Deploying your static web app to the Cloud
 
IaaS with ARM templates for Azure
IaaS with ARM templates for AzureIaaS with ARM templates for Azure
IaaS with ARM templates for Azure
 
Learning Svelte
Learning SvelteLearning Svelte
Learning Svelte
 
Ng spain
Ng spainNg spain
Ng spain
 
Angular Schematics
Angular SchematicsAngular Schematics
Angular Schematics
 
Design thinking
Design thinkingDesign thinking
Design thinking
 
Keynote ijs
Keynote ijsKeynote ijs
Keynote ijs
 
Vue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and VuexVue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and Vuex
 
Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
Kendoui
KendouiKendoui
Kendoui
 
Angular mix chrisnoring
Angular mix chrisnoringAngular mix chrisnoring
Angular mix chrisnoring
 
Nativescript angular
Nativescript angularNativescript angular
Nativescript angular
 
Graphql, REST and Apollo
Graphql, REST and ApolloGraphql, REST and Apollo
Graphql, REST and Apollo
 
Angular 2 introduction
Angular 2 introductionAngular 2 introduction
Angular 2 introduction
 
Rxjs vienna
Rxjs viennaRxjs vienna
Rxjs vienna
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
 

Kürzlich hochgeladen

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Kürzlich hochgeladen (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

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