Show Menu
Cheatography

Angular 5 Cheat Sheet (DRAFT) by

Angular 5

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

Cheatsheet version

Version
1.0.0
Last update
3/31/2018
Angular version
5

Angular CLI

Node.js version
6.9.0 or higher
npm version
3.0.0 or higher
Install Typescript
npm install -g typescript
Install Angular CLI
npm install -g @angul­ar/cli
Verify
ng --version
Help
ng --help
New project
ng new hello-­world
Run
ng serve
Run (port)
ng serve --port 9001
Generate component
ng generate component hello-­com­ponent
Generate (bluep­rints)
class, component, directive, enum, guard, interface, module, pipe, service
Build
ng build --targ­et=­pro­duction --base­-href /

Typescript data types

Boolean
isDone: boolean = false;
Decimal
decimal: number = 6;
Hex
hex: number = 0xf00d;
Binary
binary: number = 0b1010;
Octal
octal: number = 0o744;
String
name: string = 'Bob';
String (multi­-line, template)
sentence: string =
Hello, my name is ${color}
;
Array
list: number[] = [1, 2, 3];
Array (generic)
list: Array<­num­ber> = [1, 2, 3];
Tuple
x: [string, number]; x = ["he­llo­", 10];
Enum
enum Color {Red, Green, Blue}; c: Color = Color.G­reen;
Enum (number)
enum Color {Red = 1, Green = 2, Blue = 4}
Any
notSure: any = 4;
Void
function warnUs­er(): void {} // Void only accepts undefined or null
Null
n: null = null;
Undefined
u: undefined = undefined;
Never
function error(­mes­sage: string): never { throw new Error(­mes­sage); }
(let)
(You can use 'let' before variable names)
(type assert­ions)
(<s­tri­ng>­som­eValue) or (someValue as string)

Inside app.module

Declar­ations
Components in this module
Imports
Depend­encies
Providers
For dependency injection (such as services)
Bootstrap
Only for top-level component
 

Directives

Component directive
Has a template (Most common)
Structural directive
Changes DOM layout (NgFor, NgIf)
Attribute directive
Changes appear­anc­e/b­ehavior of elemen­t/c­omp­one­nt/­dir­ective (NgStyle)
NgFor (loop)
<li *ngFor­="let name of names">­Hello {{ name }}<­/li>

Passing values between components

import { ... Input } from '@angu­lar­/core';

export class UserIt­emC­omp­onent implements OnInit {
@Input() name: string; ...
}

<li *ngFor­="let name of names">
<ap­p-u­ser­-item [name]­="na­me">­</a­pp-­use­r-i­tem>
</l­i>