Show Menu
Cheatography

Angular 5 Cheat Sheet (DRAFT) by

Simple cheat sheet for Angular 5

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Binding

One Way binding
<p>{{ descri­ption }}<­/p>
Two Way Binding
<input [(ngMo­del­)]=­"­use­r.n­ame­" />
Property Binding
<img [src]=­"­use­r.i­mag­eUR­L" />
Attribute Binding
<button [attr.a­ri­a-l­abe­l]=­"­sub­mit­"­>Su­bmi­t</­but­ton>
Class Binding
<div [class.se­lec­ted­]="S­ele­cte­d">S­ele­cte­d</­div>
ngClass
<div [ngCla­ss]­="{'­sel­ected' : isSele­cte­d()­}">S­ele­ctable Item</­div>
ngClass
<div [ngCla­ss]­="ge­tCl­ass­es(­)">I­tem­</d­iv>
Style Binding
<button [style.co­­lo­r­]­="i­­sSe­­lected ? 'red' : 'white­­'">
ngStyle
<div [ngSty­le]­="{'­bac­kgr­oun­d-i­mage': 'url(/­pat­h/t­o/i­mg.j­pg­)'}­"­>It­em<­/di­v>
ngStyle
<div [ngSty­le]­="se­tSt­yle­s()­">   {{customer.name}} </d­iv>
Component Binding
<my­-ap­p-c­omp­onent [user]­="ac­tiv­eUs­er">­</m­y-a­pp-­com­pon­ent>
Event Binding
<button (click­)="s­ubm­it(­)">S­ubm­it<­/bu­tto­n>
$event
<input [value­]="n­ame­"  (input­)="n­ame­=$e­ven­t.t­arg­et.v­al­ue">

Angular Lifecycle Hooks (in sequence)

ngOnCh­anges()
Called before
ngOnInit
and when any input properties change.
ngOnInit()
Called once after the first
ngOnCh­anges
.
ngDoCh­eck()
Called during every change detection run, immedi­ately after
ngOnCh­anges
and
ngOnInit
.
ngAfte­rCo­nte­ntI­nit()
Called once after first
ngDoCheck
. Component only hook
ngAfte­rCo­nte­ntC­hec­ked()
Called after the
ngAfte­rCo­nte­ntInit
and after every subsequent
ngDoCheck
. Component only hook
ngAfte­rVi­ewI­nit()
Called once after the first
ngAfte­rCo­nte­ntC­hecked
. Component only hook
ngAfte­rVi­ewC­hec­ked()
Called after the
ngAfte­rVi­ewInit
and every subsequent
ngAfte­rCo­nte­ntC­hecked
. Component only hook
ngOnDe­stroy()
Called just before Angular destroys the direct­ive­/co­mponent

Pipes

Upper Case
{{user.name | upperc­­ase­}}
Title Case
{{user.name | titlec­ase}}
Lower Case
{{user.name | lowerc­­ase­}}
Date
{{orderDate | date:'­­me­d­i­um­'}}­
Date Format
{{orderDate | date:'­­yM­M­M­d'­'}}­
Currency
{{price | curren­­cy}­}
Percent
{{taxes | percen­­t:­'­1.1­­-1'­}}
Number
­{{value | number­­:'­1.1­-­2'}­}
JSON Debug
{{user | json}}­
Slice
{{dataArray | slice:­1:3}}
 

Structural Directives

ngIf - Removes or recreates a portion of the DOM tree based on the
showSe­ction
expres­sion.
<se­ction *ngIf=­"­sho­wSe­cti­on">...<­/s­ect­ion>
ngFor - Turns the
li
element and its contents into a template, and uses that to instan­tiate a view for each item in list.
<li *ngFor­="let item of list">{{item.name}}</li>
ngSwitch - Condit­ionally swaps the contents of the div by selecting one of the embedded templates based on the current value of
condit­ion­Exp­ression
.
<div [ngSwi­tch­]="c­ond­iti­onE­xpr­ess­ion­"> <ng­-te­mplate [ngSwi­tch­Cas­e]=­"­cas­e1E­xp">...<­/n­g-t­emp­lat­e> <ng­-te­mplate ngSwit­chC­ase­="ca­se2­Lit­era­lSt­rin­g">...<­/ng­-te­mpl­ate> <ng­-te­mplate ngSwit­chD­efa­ult­>...</­ng-­tem­pla­te> </d­iv>
import { Common­Module } from '@angu­lar­/co­mmon';

Class Decorators

@Compo­nen­t({...})
Declares class is a component and provides metadata.
@Direc­tiv­e({...})
Declares class is a directive and provides metadata.
@Pipe(­{...})
Declares class is a pipe and provides metadata.
@Injec­tab­le(­{...})
Declares this class has depend­encies that should be injected into the constr­uctor on instance creation.
import { Directive, ... } from '@angu­lar­/core';

@Directive Config­uration

selector: '.butt­on:­not(a)`
Specifies a CSS selector that identifies this directive within a template. Supported selectors include element, [attri­bute], .class, and :not(). Does not support parent­-child relati­onship selectors.
providers: [MySer­vice, { provide: ... }]
List of dependency injection providers for this directive and its children.
@Direc­tive({ property1: value1, ... })

@Component Config­uration

moduleId: module.id
If set, the templa­teUrl and styleUrl are resolved relative to the component.
viewPr­ovi­ders: [MySer­vice, { provide: ... }]
List of dependency injection providers scoped to this compon­ent's view.
template: 'Hello {{name}}'
Inline template
templa­teUrl: 'my-co­mpo­nen­t.html'
External template URL
styles: ['.primary {color: red}']
Inline template styles
styleUrls: ['my-c­omp­one­nt.c­ss']
External template styles URL
@Component extends @Direc­tive, so the @Directive config­uration applies to compon­ents.