0% found this document useful (0 votes)
68 views2 pages

Angular 2 Cheat Sheet Overview

This Angular 2 cheat sheet provides concise summaries of core Angular concepts like data binding, structural directives, lifecycle hooks, components, templates, and services. It explains how to bind data to templates, use directives like ngIf and ngFor to conditionally display parts of the template, tap into component lifecycle events, define inline and linked templates, and create and consume reusable services. The cheat sheet is a helpful two-page reference guide for Angular fundamentals.

Uploaded by

Sreeni Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Angular 2 Cheat Sheet Overview

This Angular 2 cheat sheet provides concise summaries of core Angular concepts like data binding, structural directives, lifecycle hooks, components, templates, and services. It explains how to bind data to templates, use directives like ngIf and ngFor to conditionally display parts of the template, tap into component lifecycle events, define inline and linked templates, and create and consume reusable services. The cheat sheet is a helpful two-page reference guide for Angular fundamentals.

Uploaded by

Sreeni Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Angular 2 Cheat Sheet

by Greg Finzer (GregFinzer) via [Link]/15280/cs/10781/

Binding Pipes

One Way Binding <h1>{{pageTitle}}</h1> Upper Case <p>{{[Link] | uppercase}}</p>

Two Way Binding <input [(ngModel)]="[Link]"> Lower Case <p>{{[Link] | lowercase}}</p>

Property Binding <img [src]="[Link]"> Date <p>{{orderDate | date:'medium'}}</p>

Attribute Binding <button [[Link]-label]="ok">Ok</button> Date Format <p>{{orderDate | date:'yMMMd''}}</p>

Class Binding <div [[Link]]="Selected">Selected</div> Currency <p>{{price | currency}}</p>

ngClass <div [ngClass]="setClasses()"> Percent <p>{{taxes | percent:'1.1-1'}}</p>


{{[Link]}} Number <p>value | number:'1.1-2'}}</p>
</div>
JSON Debugging <pre>{{Customer | json}}</pre>
Style Binding <button [[Link]]="isSelected ? 'red' : 'white'">

ngStyle <div [ngStyle]="setStyles()">


{{[Link]}}
</div>

Component <customer-detail [customer]="currentCustomer">


Binding <customer-detail>

Directive Binding <div [ngClass] = "{selected:


isSelected}">Customer</div>

Event Binding <button (click)="save()">Save</button>

$event <input [value]="[Link]"


(input)="[Link]=$[Link]">

Lifecycle Hooks

OnInit export class Customer implements OnInit {


ngOnInit() {}
}

OnChanges export class Customer implements OnChanges {


ngOnChanges() {}
}

AfterViewInit export class Customer implements AfterViewInit {


ngAfterViewInit() {}
}

OnDestroy export class Customer implements OnDestroy {


ngOnDestroy() {}
}

By Greg Finzer (GregFinzer) Published 6th February, 2017. Sponsored by [Link]


[Link]/gregfinzer/ Last updated 9th February, 2017. Learn to solve cryptic crosswords!
[Link] Page 1 of 2. [Link]
Angular 2 Cheat Sheet
by Greg Finzer (GregFinzer) via [Link]/15280/cs/10781/

Component with Inline Template Service (cont)

import { Component } from '@angular/core'; }


@Component({
moduleId: [Link], Injecting a Service
selector: 'customer',
//Example: [Link]
template: `
export class CustomerListComponent {
<h3>{{[Link]}}</h3>
customers: Customer[];
`

})
constructor(private customerService:
export class CustomerComponent {
CustomerService) { }
customer = { id: 100, name: 'John Smith' };
}
}

Component Linked Template


Structural Directives
import { Component } from '@angular/core';
*ngIf <div *ngIf="currentCustomer">
@Component({
Selected {{[Link]}}
moduleId: [Link],
</div>
selector: 'customer',
*ngFor <ul>
templateUrl: '[Link]',
<li *ngFor="let customer of customers">
{{ [Link] }} styleUrls: ['[Link]']

</li> })
</ul> export class CustomerComponent {

*ngSwitch <div [ngSwitch]="orderStatus"> customer = { id: 100, name: 'John Smith' };


<template [ngSwitchCase]="purchased"></template> }
<template [ngSwitchCase]="shipped"></template>
<template [ngSwitchDefault]></template>
</div>

Service

//Example: [Link]
import { HttpModule } from '@angular/http'
@Injectable()
export class CustomerService {
constructor(private http: Http) { }
getCustomers() {
return [Link]('api/customers')
.map((response: Response) =>
<Customer[]>[Link]().data)
.catch([Link]);
}
private handleError(error: Response) {
[Link](error);
return [Link]([Link]().error ||
'Service error');
}

By Greg Finzer (GregFinzer) Published 6th February, 2017. Sponsored by [Link]


[Link]/gregfinzer/ Last updated 9th February, 2017. Learn to solve cryptic crosswords!
[Link] Page 2 of 2. [Link]

You might also like