Angular in a nutshell

- 2min

Directives

Directives are classes that can be used to enhance and modify the view simply by adding HTML attributes to the template.

Built-in directives

Attribute directive: ngClass, ngStyle

Structural directive: *ngIf, *ngFor

Binding

Interpolation: {{variable}}

Property binding: [ngClass]

Event binding: (click)

Two-way binding: [input] (output) [(two-way)]

Fat arrow functions

let add = function(a, b){ return a+b; }; // classical function
let add = (a, b) => a + b; // fat arrow function

Pipes

Data-formatting: {{ currentDate | date }}

Observable

According to RxJS documentation, “it represents the idea of an invokable collection of future values or events”.

subscribe unsubscribe

It is important to not forget to unsubscribe (in ngOnDestroy function).

Promise vs Observable

Promise: single value, non-cancellable, immediate processing, two methods (then/catch)

Observable: “stream” of values, cancellable, lazy, full of operators

Callbacks

next: value => console.log(value),
error: err => console.error(err),
complete: () => console.log('DONE!')

Teardown logic: roughly speaking, provide an unsubscribe to avoid leaks, etc.

setTimeout(() => subscription.unsubscribe(), 5000);

Operators

RxJS natively provides over a hundred operators. An operator can be used to define an observable from another by applying a few transformations.

Lettable operators

map

map allows you to create a new Observable from the original one, simply by transforming each of its values

mergeMap and switchMap

These operators return an Observable for each of the values of the original Observable.

mergeMap merges the different generated Observables.

switchMap when you switch to a new value, you unsubscribe the Observable produced by the previous value (advantage over mergeMap).

Filter

filter allows you to keep only the elements for which the predicate function returns true.

Services

HttpClient simplifies the use of HTTP exchanges, and allows observables to be used as a basis.

More to discover…

StateManagement, Dispatcher, Reducer, State.