Angular 6
Angular 6
www.technicaltrainings.com
About me
Vijay Shivakumar
Designer | Developer | Trainer
www.technicaltrainings.com
About you
Designer
Developer
Architect
Business Analyst
Technology Enthusiast
www.technicaltrainings.com
Benefits Angular Advantage
Open Source
Reduction in development time
Latest Compliances
ES6
Modular
Internationalization and Accessibility
Performance
Popularity / Availability of resources
Clear Documentation
www.technicaltrainings.com
Angular Features
www.technicaltrainings.com
Setup / Tooling What to know / use ?
NodeJS Git
TypeScript MongoDB Mlab ID / Firebase ID
TraceurJS NodeMon
BableJS NVM
SystemJS
Webpack
Express
Jasmine
Karma
www.technicaltrainings.com
Angular CLI What is used…
NodeJS Git
TypeScript MongoDB Mlab ID / Firebase ID
TraceurJS NodeMon
BableJS NVM
SystemJS
Webpack
Express
Jasmine
Karma
www.technicaltrainings.com
Build Tools Using Angular-CLI
ES5 None
ES6 Traceur
BableJS
SystemJS
Webpack
Express
TypeScript TypeScript
SystemJS
Traceur / BableJS
Webpack
Express
www.technicaltrainings.com
Architecture What make Angular ?
One way data flow : Data flow only from parent to child unlike angular1
Dependency Injection : Resources are passed to a component as and when
required
Components : Creates a custom tag where the component replaces its content
Directives : Adds new functionality to HTML elements that they never had
Templates : Are separate from component
Zone.js : Manages change detection
Rendering Targets : Render an app for many devices with
Browser-platform
Browser-platform-dymanic
www.technicaltrainings.com
Angular Architecture
HEADER
NAVIGATION
MAIN HEADER
ASIDE
ARTICLE
ARTICLE
www.technicaltrainings.com
Module What is it doing..?
www.technicaltrainings.com
Component What is it ?
Component Decorator :
We can think of decorators as metadata added to our code.
When we use @Component on a class,
we are “decorating” that class as a Component.
Component Class :
Will have a constructor, properties , methods and life cycle events by default
www.technicaltrainings.com
Component What is it doing..?
www.technicaltrainings.com
Component What is it ?
HTML
template
Data
content Component
CSS
styles
www.technicaltrainings.com
Templates View
template : Inline
templateUrl : external
Display Data
Format Data
User Interaction
Avoid business logic
www.technicaltrainings.com
Styling Component v/s Page
Styling a component
styles:[`
selector { property : value; property : value }
selector { property : value; property : value }
`]
OR
stylesUrl:[ “location/style.css”, “anotherlocation/style.css” ]
Styling a page
That you define in the html page in the head or body section
www.technicaltrainings.com
Binding
Interpolation Property Binding
Keep it simple and fast (they should not take more time to compute)
Avoid multiple statements
You can not use assignment operators e.g. = , +=, ++, - - in property binding
You can not create an object e.g. new User().
www.technicaltrainings.com
Binding
Interpolation Property Binding
Keep it simple and fast (they should not take more time to compute)
Avoid multiple statements
You can not use assignment operators e.g. = , +=, ++, - - in property binding
You can not create an object e.g. new User().
www.technicaltrainings.com
Event Binding Statement Binding
Keep it simple and fast (they should not take more time to compute)
The callback functions can take a single parameter which is referred as $event
( If you wish to send multiple params you can wrap them in an object and send )
Avoid business logic on templates
You can not create an object e.g. new User().
www.technicaltrainings.com
Events Element events supported
www.technicaltrainings.com
Style Binding class / ngClass
[class.className] =“stronghero”
[ngClass]={ expression that returns a string, object or an array}
www.technicaltrainings.com
Style Binding style/
As a property binding
[style.color] =“ ‘#333’ ” or [style.background-color] =“ ‘yellow’ ”
[ngStyle]={ expression that returns a style value}
In the example below the ternary operator will return a style property and
value combination
Eg;
[ngStyle]="{‘color’: ‘red’, ‘background-color’ : ’gray’}”
[ngStyle]="{‘color’: heroPower > 5 ? ‘green’ : ‘red’}”
www.technicaltrainings.com
Local Variables Temporary variables
www.technicaltrainings.com
Directive Structural Directives
*ngIf
*ngFor
*ngSwitch
NgIf *ngIf
NgSwitch *ngSwitch
NgFor *ngFor
NgStyle [ngStyle]
NgClass [ngClass]
NgNonBindable ngNonBindable
www.technicaltrainings.com
Structural
Directive Built in Directives
Adds or removes the DOM contents or they change the structure of DOM
hence the name
<ul>
<li *ngFor = “let user of users”> </li>
</ul>
In this example it shall loop over and for users
and create a temp variable user in the scope of the loop
For every loop the li and its content is repeated
<li *ngIf=“true/false”>{{ data }}</li> OR <li [hidden]=“true/false”>{{ data }}</li>
www.technicaltrainings.com
Structural
Directive Built in Directives
Adds or removes the DOM contents or they change the structure of DOM
hence the name
<ul>
<li *ngFor = “let user of users”> </li>
</ul>
In this example it shall loop over and for users
and create a temp variable user in the scope of the loop
For every loop the li and its content is repeated
<li *ngIf=“true/false”>{{ data }}</li> OR <li [hidden]=“true/false”>{{ data }}</li>
www.technicaltrainings.com
Structural
Directive The ng-template way
*ngIf
*ngFor
*ngSwitch
NgIf
NgSwitch
NgStyle
NgClass
NgFor
NgNonBindable
www.technicaltrainings.com
Structural
Directive Switch
<div [ngSwitch]="hero.power">
<h1 *ngSwitchCase="8">Strong</h1>
<h1 *ngSwitchCase="7">Weak</h1>
<h1 *ngSwitchDefault>Recovering</h1>
</div>
www.technicaltrainings.com
Binding
Summary When to use what ?
www.technicaltrainings.com
Communication Input & Outputs
www.technicaltrainings.com
Communication alias
www.technicaltrainings.com
Pipes Format your output
www.technicaltrainings.com
Life Cycle Hook Angular Events
ngOnChange :
happens when ever the component is changed via an input directive
ngOnInit : happens only once when the component is initialized called only once
ngDoCheck : happens after the ngOnChange is called you can here access the old
value and the new value for the property that changed Called during every
change detection before ngOnChanges() and ngOnInit()
ngAfterContentInit : Called once after the first ngDoCheck()
ngAfterContentChecked :
ngAfterViewInit :
ngAfterViewChecked :
ngOnDestroy :
www.technicaltrainings.com
Services Dealing with Data
www.technicaltrainings.com
Services How to Create ?
1 Build a service
2 Register the service
3 Inject the service
www.technicaltrainings.com
DI Inversion of control
www.technicaltrainings.com
Dependency
Injection Dealing with Data
www.technicaltrainings.com
Forms Forms & User Inputs
www.technicaltrainings.com
Forms FormControl
FormControl
Represents a single input field - it is the smallest unit of an Angular form.
www.technicaltrainings.com
Forms FormGroup
FormGroup
Provides a wrapper interface around more than one FormControls so we
can manage multiple fields to validate them.
www.technicaltrainings.com
Template Forms Forms & User Inputs
Easy to use
Similar to legacy forms
Uses 2 way data binding
Minimal component code
Automatically track form input element and state
www.technicaltrainings.com
Reactive Forms Forms & User Inputs
More Flexible
Used for complex scenarios
Model is immutable
Easier to perform action upon value change
Reactive Transformations (debounce)
Add input elements dynamically
Unit test forms
www.technicaltrainings.com
Validation Forms & User Inputs
www.technicaltrainings.com
Validation States
www.technicaltrainings.com
HTTP Loading Data
www.technicaltrainings.com
RXJS Observables
www.technicaltrainings.com
Rxjs Principles
www.technicaltrainings.com
Store What is it?
www.technicaltrainings.com
NgRx Observables
www.technicaltrainings.com
Routing Navigating / Multipage
Define Base Path : implemented by default with angular-cli <base href=“/” />
recommended to be the first child of head tag
Import Router : import { Routes } from '@angular/router';
Configure Routes : export let ROUTES: Routes = [
{ path: '', component: HomeComponent },
{ path: about', component: AboutComponent }
];
Place Templates : <router-outlet>
Activate Routes : navigate to that path in browser url
www.technicaltrainings.com
Unit Testing Fundamentals
www.technicaltrainings.com
www.technicaltrainings.com