Say “Hello World” to Angular 2
Example
Writing a Simple Angular 2 Component
|
|
Using events and refs
|
|
Events in Depth
|
|
- eventName: click, mouseover, …
- eventHandler: function
- argument: $event, variable, …
Injecting a Service
todo-service.ts
1234567import { Injecttable } from 'angular2/core';@Injectable()export class TodoService {todos = [];}man.ts
1234...imrpot { TodoService } from './todo-service';...boostrap(App, [TodoService]);todo-input.ts
12345678910111213...imrpot { TodoService } from './todo-service';...export class TodoInput {constructor(todoService: TodoService) {this.todoService = todoService;}onClick(value) {this.todoService.todos.push(value);console.log(this.todoService.todos);}}
Using the @Inject decorator
|
|
Using ng-for to repeat template elements
|
|
Using ng-model for two-way binding
|
|
Adding a data model
|
|
Template property syntax
|
|
- property
- ngClass, hidden, contentEditable, …
- propertyValue
- boolean, string, …
- Example12<span [hidden]="'todo.status == 'completed'">{{ todo.title }}</span><button (click)="todo.toggle()">Toggle</button>
Passing data to components with @Input
todo-list.ts
1<todo-item-renderer [todo]="todo"></todo-item-renderer>todo-item-renderer.ts
123456789101112131415import { Component, Input } from 'angular2/core';@Component({selector: 'todo-item-renderer',template: `<div><span [hidden]="'todo.status == completed'">{{ todo.title }}</span><button (click)="todo.toggle()">Toggle</button></div>`})export class TodoItemRenderer {@Input() todo;}
ng-class and Encapsulated Component Styles
|
|
Controlling how Styles are Shared with View Encapsulation
- ViewEncapsulation
- None, Native, Emulated
|
|
Using Pipes to Filter Data
json, uppercase, …
search-pipe.ts
1234567891011import { Pipe } from 'angular2/core';@Pipe({name: 'search'})export class SearchPipe {transform(value) {return value.filter((item) => item.title.startsWith('s'));}}todo-list.ts
123456789@Component({pipes: [SearchPipe],...template:`... todos | search ...`})...
Using Array …spread to enforce Pipe immutability.
|
|
Refactoring mutations to enforce immutable data in Angular 2
started-pipe.ts
1234567891011import { Pipe } from 'angular2/core';@Pipe({name: 'started'})export class SearchPipe {transform(value) {return value.filter((item) => 'started' === item.status);}}todo-item-renderer.ts
12345678910import { Ouput, EventEmitter } from 'angular2/core';...<button (click)="toggle.emit(todo)">Toggle</button>...export class TodoItemRenderer {@Input() todo;@Output() toggle = new EventEmitter();}todo-list.ts
123...(toggle)="todoService.toggleTodo($event)"...todo-service.ts
123456789101112...toggleTodo(todo: TodoModel) {todo.toggle();const i = this.todos.indexOf(todo);this.todos = [...this.todos.slice(0, i),todo,...this.todos.slice(i + 1)];}...
Build a select dropdown with *ngFor in Angular 2
started-pipe.ts
1234567891011import { Pipe } from 'angular2/core';@Pipe({name: 'started'})export class SearchPipe {transform(value, [status]) {return value.filter((item) => item.status === status);}}todo-list.ts
12345678910...<li *ngFor="#todo of todoService.todos | started: status">...export class TodoList {@Input() status;...}...main.ts
1234...<status-selector (select)="status = $event"><status-selector><todo-list [status]="status'"></todo-list>...status-selector.ts
1234567891011121314151617181920212223import { Component, Output, EventEmitter } from 'angular2/core';@Component({selector: 'status-selector',template: `<div><select #sel (change)="select.emit(sel.value)><option *ngFor="#status of statuses">{{ status }}</option></select></div>`})export class StatusSelector {@Output select = new EventEmitter;statuses = ['started', 'completed'];ngOnInit() {this.select.emit(this.statuses[0]);}}
Filter items with a custom search Pipe in Angular 2
search-pipe.ts
1234567891011import { Pipe } from 'angular2/core';@Pipe({name: 'search'})export class SearchPipe {transform(value, [term]) {return value.filter((item) => item.title.startsWith(term));}}todo-list.ts
123456789101112...<li *ngFor="#todo of todoService.todos| started: status| search: term">...export class TodoList {@Input() status;@Input() term;...}...search-box.ts
123456789101112131415161718import { Component, Output, EventEmitter } from 'angular2/core';@Component({selector: 'search-box',template: `<div><input #input type="text" (input)="update.emit(input.value)"></div>`})export class SearchBox {@Output() update = new EventEmitter();ngOnInit() {this.update.emit('');}}main.ts
1234567...<search-box (update)="term = $event"></search-box><todo-list[status]="status"[term]="term"></todo-list>...
Organizing Angular 2 projects by feature
- refactor folder structure…
- types:
- components
- pipes
- services
Overview of Angular 2 and what to learn next…
- Component
- directive
- Input
- @Input() input <—> [input]=”input”
- Output
- @Output() output <—> (output)=”outputHandler($event)”
- EventEmitter
- ngFor
- event
- data binding
- property
- Service
- data
- Pipe
- transform
- EventEmitter