import {Component} from 'angular2/core';
@Component({
...
})
class Component {
constructor() {}
}import {Component} from 'angular2/core';
@Component({
selector: 'my-first-component',
template: `<h1>This is my first component</h1>`
})
class MyComponent {
constructor() {}
}import {bootstrap} from 'angular2/platform/browser';
import {MyComponent} from './my-component.component';
bootstrap(MyComponent) // Additional parameter is an array of providers
.catch( err => console.log(err));
One small thing is missing. Can you guess?
<!DOCTYPE html>
<html>
<head>
<script src="src/my-angular-bootstrap.js"></script>
</head>
<body>
<my-first-component></my-first-component>
</body>
</html><img [src]="..." />
<img src="..." /> <!-- similar to: <img [src]="'...'" />
<div [disbaled]="'true'"></div>
<button (click)="onSave()">On Save</button> <!-- "on-click" is an alternate syntax -->
<input [value]="currentHero.firstName"
(input)="currentHero.firstName=$event.target.value" >
<my-component (myEvent)="doSomething($event)"></my-component><button [attr.aria-label]="actionName">{{actionName}} with Aria</button>
<table border=1>
<!-- expression calculates colspan=2 -->
<tr><td [attr.colspan]="1 + 1">One-Two</td></tr>
<!-- ERROR: There is no `colspan` property to set!
<tr><td colspan="{{1 + 1}}">Three-Four</td></tr>
-->
<tr><td>Five</td><td>Six</td></tr>
</table><!-- standard class attribute setting -->
<div class="bad curly special">Bad curly special</div>
<!-- reset/override all class names with a binding -->
<div class="bad curly special"
[class]="badCurly">Bad curly</div>
<!-- toggle the "special" class on/off with a property -->
<div [class.special]="isSpecial">The class binding is special</div>
<!-- binding to `class.special` trumps the class attribute -->
<div class="special"
[class.special]="!isSpecial">This one is not so special</div>
<button [style.color] = "isSpecial ? 'red' : 'green'">Red</button>
<button [style.backgroundColor]="canSave ?'cyan' : 'grey'" >Save</button>
<button [style.fontSize.em]="isSpecial ? 3 : 1" >Big</button>
<button [style.fontSize.%]="!isSpecial ? 150 : 50" >Small</button>
<hero-detail [hero]="currentHero"></hero-detail>
<hero-detail (deleteRequest)="deleteHero()"></hero-detail>
<div (myClick)="clicked=$event">click me</div>
[textContent]="interpolate(...)
<p>Hello {{name}}</p>
<p [textContent]="interpolate(['Hello'], [name])"></p> <!-- same as above -->
| Binding Type | Target | Example |
|---|---|---|
| Interpoluation | Element property | <div>{{name}}</div> |
| Property | Element property Component property Directive property |
<img [src] = "heroImageUrl"> <hero-detail [hero]="currentHero"></hero-detail> <div [ngClass] = "{selected: isSelected}"></div> |
| Event | Element event Component event Directive event |
<button (click) = "onSave()">Save</button> <hero-detail (deleteRequest)="deleteHero()"></hero-detail> <div (myClick)="clicked=$event">click me</div> |
| Attribute | Attribute (the exception) | <div [class.special]="isSpecial">Special</div> |
| Class | class property | <div [class.special]="isSpecial">Special</div> |
| Style | style property | <button [style.color] = "isSpecial ? 'red' : 'green'"> |
template: `
<div>
<img src="{{heroImageUrl}}">
<span [style.text-decoration]="lineThrough">
{{prefix}} {{hero?.fullName}}
</span>
<button (click)="delete()">Delete</button>
</div>`<!-- phone refers to the input element; pass its `value` to an event handler -->
<input #phone placeholder="phone number">
<button (click)="callPhone(phone.value)">Call</button>
<!-- fax refers to the input element; pass its `value` to an event handler -->
<input var-fax placeholder="fax number">
<button (click)="callFax(fax.value)">Fax</button>
<form (ngSubmit)="onSubmit(theForm)" #theForm="ngForm">
<button type="submit" [disabled]="!theForm.form.valid">Submit</button>
</form>
<template ngFor #hero [ngForOf]="heroes" [ngForTrackBy]="trackByHeroes">
<hero-detail [hero]="hero"></hero-detail>
</template>// Defined in Component
selector: 'my-component',
template: `<div>I am component</div>
<ng-content></ng-content>`
// Used in parent component template:
template: `<my-component>This is my Content</my-component>
import {Component, Input} from 'angular2/core';
@Component({
selector: 'my-component',
template: `<h1>My Component</h1>`,
inputs: ['myOtherColor:othercolor']
})
class MyComponent() {
@Input('color') myColor : string;
constructor() {
console.log(this.myColor + ' ' + this.myOtherColor);
}
}template: `<div>
<my-component [color]="'lightblue'" [othercolor]="'lightyellow'">
</my-component>
</div>`import {Component} from 'angular2/core';
@Component({
selector: 'counter',
template: `// omitted for brevity`,
outputs: ['counterChange:change']
})
export class CounterComponent {
public counterValue = 0;
increment() {
this.counterValue++;
this.counterChange.emit({value: this.counterValue})
}
decrement() {
this.counterValue--;
this.counterChange.emit({value: this.counterValue})
}
}<counter (change)="myValueChange($event);"></counter><!-- NgClass -->
<div [ngClass]="{active: isActive}">
<div [ngClass]="{active: isActive, shazam: isImportant}">
<div [class.active]="isActive">
<!-- NgStyle -->
<div [ngStyle]="{color: colorPreference}">
<div [style.color]="colorPreference">
<!-- NgIf -->
<table *ngIf="movies.length">
<!-- NgFor -->
<tr *ngFor="#movie of movies">
<template ngFor #hero [ngForOf]="heroes" [ngForTrackBy]="trackByHeroes">
<hero-detail [hero]="hero"></hero-detail>
</template>
<!-- NgSwitch -->
<span [ngSwitch]="favoriteHero && checkMovieHero(favoriteHero)">
<p *ngSwitchWhen="true">Excellent choice!</p>
<p *ngSwitchDefault>Please enter your favorite hero</p>
</span>
// Parent Component
import {Component} from 'angular2/core';
import {ChildComponent} from './my-child.component';
@Component({
selector: 'my-component',
template: `<child-component [param]="param" (childevent)="onChildEvent($event)">
This is child Content
</child-component>`,
directives: [ChildComponent]
})
class MyComponent() {
onChildEvent(data) { consloe.log(data); }
}// Child Component
import {Component} from 'angular2/core';
@Component({
selector: 'child-component',
template: `<div>I am a child</div><ng-content></ng-content>`,
})
export class ChildComponent() {
@Input() param : any;
@Output() childevent : EventEmitter<any> = new EventEmitter();
}It is advisable to do initialization in Component OnInit hook and not constructor (some object are still not initialized)
| Hook | Timing | Component Directive |
|---|---|---|
| ngOnChanges | before ngOnInit and when a data-bound input property value changes | C, D |
| ngOnInit | after the first ngOnChanges | C, D |
| ngDoCheck | during every Angular change detection cycle | C, D |
| ngAfterContentInit | after projecting content into the component | C |
| ngAfterContentChecked | after every check of projected component content | C |
| ngAfterViewInit | after initializing the component's views and child views | C |
| ngAfterViewChecked | after every check of the component's views and child views | C |
| ngOnDestroy | just before Angular destroys the directive/component | C, D |
import {Component, OnInit} from 'angular2/core';
@Component({
})
class MyComponent implements OnInit {
constructor() {}
ngOnInit() {
console.log('Component init');
}
}