Santosh Yadav
Google Developer Expert for Angular GitHub Start and Nx Champion, Open source contributor for Angular and NgRx, creator ng deploy for Netlify, NestJSAddons core team.
@SantoshYadavDev
TechTalksWithSantosh
GDE for Angular, GitHub Star, Auth0 Ambassador
Writer inDepth.dev, ThisIsAngular, ThisIsLearning
Santosh Yadav
Angular is built keeping productivity in mind.
Getting Started
npm i @angular/cli -g //install Angular CLI
ng new <app-name> //create a new app
ng serve //serve the app on localhost
ng build --prod //create production build
ng deploy //deploy application to your cloud provider
//(Azure, AWS, Firebase, Netlify, GitHub Pages etc)
CLI helps you from development to deployment
Template
Its just HTML
{{variableName}} // string interpolation
<div [innerText]="variableName"></div>
// Property Binding
<button (click)="function()">Click</button>
// Event Binding
Dependency Injection
DI out of the box
export class ProductComponent {
// inject a class
constructor(private service: ProductService) { }
}
// creating a token
export const BROWSER_STORAGE =
new InjectionToken<Storage>('Browser Storage', {
providedIn: 'root',
factory: () => localStorage
});
export class BrowserStorageService {
// injecting a token
constructor(@Inject(BROWSER_STORAGE) public storage: Storage) { }
}
Forms
Template Driven Forms
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
// other imports ...
FormsModule
],
})
export class AppModule { }
Reactive Forms
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
// other imports ...
ReactiveFormsModule
],
})
export class AppModule { }
Creating Forms
<form [formGroup]="filterForm" (submit)="filterProduct()">
<div>
<input formControlName="minPrice" placeholder="Min Price">
</div>
<div>
<input formControlName="maxPrice" placeholder="Min Price">
</div>
<button type="submit">
Filter
</button>
</form>
Routing
How to setup router
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'event', component: EventComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
<a [routerLink]="['home']">Home</a>
<a [routerLink]="['events']">Events</a>
<router-outlet></router-outlet>
What if we have a large App
Lazy Loading
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{
path: 'events',
loadChildren: () => import('./events/events.module')
.then(m => m.EventsModule)
},
{ path: '', redirectTo: 'home', pathMatch: 'full' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Handling API call
Using HTTP module
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
// other imports ...
HttpClientModule
],
})
export class AppModule { }
--------------------------------------------------------------
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class EventsService {
constructor(private http: HttpClient) { }
getEvents() {
return this.http.get<T[]>('api/events');
}
}
Web Components
ng add @angular/elements --project=*your_project_name*
To setup Angular Elements
import { Component, Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { PopupService } from './popup.service';
import { PopupComponent } from './popup.component';
@Component({
selector: 'app-root',
template: `
<input #input value="Message">
<button (click)="popup.showAsComponent(input.value)">Show as component</button>
<button (click)="popup.showAsElement(input.value)">Show as element</button>
`,
})
export class AppComponent {
constructor(injector: Injector, public popup: PopupService) {
// Convert `PopupComponent` to a custom element.
const PopupElement = createCustomElement(PopupComponent, {injector});
// Register the custom element with the browser.
customElements.define('popup-element', PopupElement);
}
}
Small API to convert component to element
Creating PWA
ng add @angular/pwa
Make your App PWA
Localize your App
ng add @angular/localize
Configure localization
<h1 i18n="site header|An introduction header for this sample">Hello i18n!</h1>
// extract the message from html
ng xi18n
Support 3 formats
Unit Testing
Server Side Rendering
ng add @nguniversal/express-engine
Configure SSR
Code Sharing and Authoring Angular Libraries
ng generate lib <libname>
ng build <libname> --prod
cd dist/<libname>
npm publish --access=public
Angular compiler
Angular Dev Tools
Angular Language Service
Angular Material and CDK
// add Angular Material to your application
ng add @angular/material
Bring/Build Your own Tool
Schematics
Builders
Want to be super productive
ng add @nrwl/workspace
References
@SantoshYadavDev
TechTalksWithSantosh
Thank You
By Santosh Yadav
Google Developer Expert for Angular GitHub Start and Nx Champion, Open source contributor for Angular and NgRx, creator ng deploy for Netlify, NestJSAddons core team.