Angular2 Cheatsheet
Angular2 Cheatsheet
Angular2 Cheatsheet
Data Binding
Module
Value
3.1415
Metadata
Module
Fn
Angular 2 is a framework to help us build client applications for the Web and mobile.
Core Features:
Template
Directive
Property
Binding
Metadata
Injector
Service
Component
Event
Binding
{{value}}
[property] = value
(event) = handler
[(ngModel)] = property
Angular supports data binding, a mechanism for coordinating parts of a template with parts of a component. We add
binding markup to the template HTML to tell Angular how to
connect both sides. There are four forms of data binding
syntax. Each form has a direction to the DOM, from the
DOM or in both directions as indicated by the arrows in the
diagram. Lets take a variation of our TodoListComponent
again to explain the various data bindings:
app/todo-list.component.html (variation)
<div>{{todo.name}}</div>
<todo-detail [todo]="selectedTodo"></todo-detail>
<div (click)="selectTodo(todo)"></div>
The interpolation displays the components todo.name property value within the <div> tags.
The [todo] property binding passes the selectedTodo from the parent TodoListComponent to the todo
property of the child TodoDetailComponent.
The (click) event binding calls the components selectTodo method when the user clicks on a Todos name.
Two-way data binding is an important fourth form that combines property and event binding in a single notation using
the ngModel directive. Heres an example from the TodoDetailComponent:
<input [(ngModel)]="todo.name">
In two-way data binding, a data property flows to the input box from the component as with property binding. The
users changes also flow back to the component, resetting the property to the latest value, as with event binding.
vi
ce
Module
Service
{ }
Se
r
Module
Component
{ }
Dependency Injection
Component
Overview
DOM / HTML
ANGULAR 2 in TypeScript
ANGULAR 2
Component
{constructor(service)}
Dependency Injection is a way to supply a new instance of a class with the fully
formed dependencies it requires. Most dependencies are services. Angular uses
dependency injection to provide new components with the services they need.
In TypeScript Angular can tell which service a component needs by looking at
the types of its constructor parameters. For example, the constructor of our
TodoListComponent needs the TodoService:
app/todo-list.component.ts (constructor)
constructor(private service: TodoService) { }
When Angular creates a component, it first asks an injector for the services that the component requires. An injector
maintains a container of service instances that it has previously created. If a requested service instance is not in the
container, the injector makes one and adds it to the container before returning the service to Angular. When all
requested services have been resolved and returned, Angular can call the components constructor with those services
as arguments. This is what we mean by dependency injection. Although we can register classes at the root level while
bootstrapping the application, the preferred way is to register at component level within the @Component metadata,
in which case we get a new instance of the service with each new instance of that component and its child components.
app/todo-list.component.ts (excerpt)
@Component({
providers: [TodoService]
})
export class TodoListComponent { ... }
Note: Unlike all component classes we have to mark all injectable classes with the @Injectable decorator. This is also
true for our TodoService:
The Template
The Module
Module
Component
{ }
Angular apps are modular. In general we assemble our application from many modules.
A typical module is a cohesive block of code dedicated to a single purpose. A module exports
something of value in that code, typically one thing such as a class.
Template
The Directive
We define a components view with its companion template. A template is a form of HTML
that tells Angular how to render the component.
Heres a template for our TodoListComponent, extracted in its own file:
Directive
app/app.component.ts (excerpt)
app/todo-list.component.html
<h2>Todo List</h2>
app/main.ts (excerpt)
import {AppComponent} from './app/component';
The import statement tells the system it can get an AppComponent from a module named app.component located
in a neighbouring file. The module name is often the same as the filename without its extension. Angular uses an
external module loader like SystemJs to bind all the exports and imports together. SystemJS is not the only module
loader that will work with Angular 2. Other module loaders, such as WebPack, can be swapped in instead. Which one
you take is up to you, but a module loader is crucial for every Angular application.
If the template isnt too big, we can embed it directly into the @Component metadata. Please note that such embedded
markup has to be placed between two back ticks, especially when it spans more than one line.
Like the <todo-detail> tag from the example above, which represents the view of another component, we can
arrange our components and templates in a hierarchical manner to build out our richly featured application.
Child A
Component
A component controls a patch of screen real estate that we could call a view. The shell at the
application root with navigation links, the list of Todos, the Todo-editor, they're all views
controlled by components. We define a components application logic what it does to
support a view inside a class. The class interacts with the view through an API of properties
and methods.
Component
Angular ships with a few other directives that either alter the layout structure (e.g. ngSwitch) or modify aspects of
DOM elements and components (e.g. ngStyle and ngClass).
Root
Template
Child A
Template
Child B
Component
Child B
Template
Grandchild
Component
Grandchild
Template
ngOnInit() {
this.todos = this.service.getTodos();
}
selectTodo(todo: Todo) {
this.selectedTodo = todo;
}
Angular creates, updates and destroys components as the user moves through the application. The developer can take
action at each moment in this lifecycle through optional Lifecycle Hooks like OnInit (shown above), AfterContentInit,
AfterViewInit or OnDestroy.
Library Modules
Library Module
Service
{ }
Directive
{ }
Value
3.1415
Fn
There is nothing specifically Angular about services. Angular itself has no definition of a
service. There is no ServiceBase class. Yet services are fundamental to any Angular
application. Heres a TodoService that fetches Todo items and returns them in a resolved promise. The TodoService
depends on a LoggerService and another BackendService that handles the server communication.
Metadata
Metadata tells Angular how to process a class. The TodoListComponent is just a class. Its
not a component until we tell Angular about it. This is done by attaching metadata to the
class. Heres some metadata for TodoListComponent:
app/todo-list.component.ts (metadata)
Some modules are libraries of other modules. Angular itself ships a collection of library modules called barrels. Each Angular library is actually a
public faade over several logically related private modules.
The angular2/core library is the primary Angular library module from
which we get most of what we need. Other important library modules are
angular2/common, angular2/http, and angular2/routing. We
import what we need from an Angular library module in much the same
way as our own modules:
Service is a broad category encompassing any value, function or feature that our application needs. Almost anything can be a service. A service is typically a class with a narrow,
well-defined purpose. It should do something specific and do it well.
app/todo.service.ts
Angular Metadata
@Component({
selector: 'todo-list',
templateUrl: 'app/todo-list.component.html',
directives: [TodoDetailComponent],
providers: [TodoService]
})
export class TodoListComponent { ... }
The @Component decorator identifies the class immediately below it as a component class. Here we see some of the
most commonly used @Component configuration options:
selector a css selector that tells Angular to create an instance of this component where it finds a <todo-list>
tag in the parent HTML. If the template of the application shell contains
@Component({ ... })
@RouteConfig([
{path: 'projects', name: 'Projects', component: ProjectComponent},
{path: 'todos', name: 'ToDos', component: TodoListComponent},
{path: 'todos/:id', name: 'ToDo', component: TodoDetailComponent},
]}
export class AppComponent { }
With the routing in place, we can define the navigation in our AppComponent template:
template:
<nav>
<a [routerLink]="['Projects']">Projects</a>
<a [routerLink]="['ToDos']">ToDos</a>
</nav>
<router-outlet></router-outlet>
The <router-outlet> tag defines the place where the component views will be rendered. When working with
routing and navigation, dont forget to include in your index.html:
index.html (excerpt)
An Angular application needs to be bootstrapped to make dependency injection work and hook up the application
lifecycle. This is done in our main class:
app/main.ts (excerpt)
HTTP Client
import {bootstrap}
from 'angular2/platform/browser';
import {AppComponent} from './app.component';
HTTP is the primary protocol for browser/server communication. We use the Angular HTTP client to communicate via
XMLHttpRequest (XHR) with the server. Lets first have a look at our TodoService:
app/todo-service.ts
import {Http, Response} from 'angular2/http';
import {Observable}
from 'rxjs/observable';
import {Todo}
from './Todo';
Pipes
A pipe takes in data as input and transforms it to a desired output. Well illustrate by transforming a
component's due date property into a human-friendly date:
The Service
Service
app/todo-list.component.ts
toDos: Todo[];
selectedToDo: Todo;
<input [(ngModel)]="todo.name">
app/app.component.ts (excerpt)
<script src="node_modules/angular2/bundles/router.dev.js"></script>
In this example, we configure the top-level AppComponent with three route definitions:
@Injectable
export class TodoService { ... }
bootstrap(AppComponent);
Root
Component
Our Angular templates are dynamic. When Angular renders them, it transforms the DOM
according to the instructions given by a directive. A directive is a class with directive metadata. We already met one form of directive: the @Component. A component is a directivewith-a-template and the @Component decorator is actually an @Directive with templateoriented features. There are two other kinds of directives as well that we call structural and
attribute directives.
Structural directives alter the layout by adding, removing or replacing elements in the DOM:
The Component
Component
{ }
Metadata
app/todo-service.ts (excerpt)
In most applications, users navigate from one view to the next as they perform application tasks. When the browsers
URL changes, the router looks for a corresponding route definition from which it can determine the component to
display. A router has no route definitions until we configure it. The preferred way to simultaneously create a router and
add routes is with an @RouteConfig decorator applied to the routers host component.
}
Our components are big consumers of services. They depend upon services to handle most chores. They dont fetch
data from the server, they dont validate user input, they dont log directly to the console. They delegate such tasks to
services. A components job is to enable the user experience and nothing more. It mediates between the view
(rendered by the template) and the application logic (which often includes some notion of a model). A good
component presents properties and methods for data binding. It delegates everything non-trivial to services.
Inside the interpolation expression we flow the components due date value through the pipe separator (|) to the date
pipe function on the right. All pipes work this way. You may also provide optional pipe parameters like the exact date
format as shown in the example above. Angular comes with a stock set of pipes such as DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe and PercentPipe. They are all immediately available for use in any
template. Of course, you can write your own custom pipes:
}
As we can see, the Json response from the server is mapped into an array of Todos with the .json() function. The
result of getTodos() is a cold observable of Todos, which means the original HTTP request wont go out until something subscribes to the observable. That something is our TodoListComponent:
app/exponential-strength.pipe.ts
app/todo-list.component.ts (ngOnInit)
ngOnInit() {
this.service.getTodos()
.subscribe(
todos => this.todos = todos,
error => this.errorMessages = <any>error);}
@Pipe({name: 'exponentialStrength'})
export class ExponentialStrengthPipe implements PipeTransform {
transform(value:number, args:string[]) : any {
return Math.pow(value, parseInt(args[0] || '1', 10));
}
}
app/todo-service.ts (addTodo)
Now we need a component to demonstrate our pipe:
addTodo(name: string): Observable<Todo> {
let body = JSON.stringify({ name });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
app/exponential-strength.component.ts
import {Component} from 'angular2/core';
import {ExponentialStrengthPipe} from './exponential-strength.pipe';
@Component({
selector: 'power-booster',
template: '<p>Super power boost: {{2 | exponentialStrength: 10}}</p>',
pipes: [ExponentialStrengthPipe]
})
export class PowerBooster { }
Note that we have to include our pipe in the pipes configuration option of the @Component.
app/todo-list.component.ts (addTodo)
addTodo(name: string) {
if (!name) { return; }
<todo-list></todo-list>
Angular inserts an instance of the TodoListComponent view between those tags.
templateURL the address of this components template
template an inline HTML view directly written within the @Component decorator
directives an array of components that this template requires. In our sample its TodoDetailComponent
which defines the view <todo-detail>
pipes an array of pipes that this template requires
providers an array of dependency injection providers for services this component requires. Classes in this array
are typically injected into the components constructor
We apply other metadata decorators in a similar fashion to guide Angular behaviour. The @Injectable, @Input,
@Output, @RouterConfig are few of the more popular decorators.
this.service.addTodo(name)
.subscribe(
todo => this.todos.push(todo),
error => this.errorMessages = <any>error);
Resources
Angular provides a library named ngUpgrade to help you run both versions of Angular in parallel. This allows you to
migrate your application step by step. In particular it allows you to use Angular 1 directives in Angular 2 components
(upgrade) and use Angular 2 components in
Angular 1 directives (downgrade). Youll find further
Preparation
Migration
information about upgrading your existing Web
application in the official Angular 2 docs [2] and on
Angular 1
the thoughtram blog [6].
Angular 2
}
When working with the HTTP client, dont forget to include in your index.html:
index.html (excerpt)
<script src="node_modules/angular2/bundles/http.dev.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
Legal Disclaimer: While we have made every attempt to ensure that the information in this publication has been obtained from reliable sources, bbv Software Services (bbv) is not responsible for any errors or omissions, or for the results obtained from the use of this information. All information is provided with no guarantee of completeness or accuracy, and without warranty of any kind. In no event will bbv or its employees therefore be liable to you or anyone else for any decision made or action taken in reliance on the information in this publication. The information in this publication should not be used as a substitute for consultation with professional bbv advisors. Before making any decision or taking any action, you should consult a bbv professional. The names of actual companies and products (e.g. Google, bbv Software Services) mentioned in this publication may be the trademarks of their respective owners. 2016 bbv Software Services
This poster is a derivative of Angulars documentation page [2] by Google, used under CC BY 4.0. The poster is licensed under CC BY 4.0 by bbv Software Services AG.
www.bbv.ch info@bbv.ch