Created
March 21, 2016 12:22
-
-
Save zhentian-wan/a0e64cb65c2da4c15acd to your computer and use it in GitHub Desktop.
Angular 2 Pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from 'angular2/core'; | |
import {bootstrap} from 'angular2/bootstrap'; | |
import {TodoService} from './TodoService'; | |
import {TodoInput} from './TodoInput'; | |
import {TodoList} from './TodoList'; | |
@Component({ | |
selector: 'app', | |
directives: [TodoInput, TodoList], | |
template: ` | |
<todo-input></todo-input> | |
<todo-list></todo-list> | |
` | |
}) | |
class App { | |
} | |
// []: we can inject service which can be used for the whole app | |
bootstrap(App, [ | |
TodoService | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Pipe} from 'angular2/core'; | |
@Pipe({ | |
name: 'search' | |
}) | |
export class SearchPipe{ | |
transform(todos){ | |
return todos.filter( | |
(todoModel) => { | |
// Only showing the todo starts with 'e' | |
return todoModel.title.startsWith('e'); | |
} | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from 'angular2/core'; | |
import {TodoService} from './TodoService'; | |
import {TodoModule} from './TodoModule'; | |
@Component({ | |
selector: 'todo-input', | |
template: ` | |
<form (submit)="onSubmit()"> | |
<input type="text" [(ngModel)]="todoModule.title"> | |
</form> | |
` | |
}) | |
export class TodoInput { | |
// Init a todoModule | |
todoModule:TodoModule = new TodoModule(); | |
constructor(public todoService:TodoService) { | |
} | |
onSubmit() { | |
this.todoService.todos.push(this.todoModule); | |
// After insert, create new TodoModule | |
this.todoModule = new TodoModule(); | |
console.log(this.todoService.todos); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, Input, ViewEncapsulation} from 'angular2/core'; | |
@Component({ | |
selector: 'todo-item-renderer', | |
// encapsulation:ViewEncapsulation.Emulated, //Default, (parent style pass )in and no (child style go) out | |
// encapsulation:ViewEncapsulation.Native, // no in and no out | |
encapsulation:ViewEncapsulation.None, // in and out | |
template: ` | |
<style> | |
.completed{ | |
text-decoration: line-through; | |
} | |
</style> | |
<div> | |
<span [ngClass]="todo.status" | |
[contentEditable]="todo.isEdit">{{todo.title}}</span> | |
<button (click)="todo.toggle()">Toggle</button> | |
<button (click)="todo.edit()">Edit</button> | |
</div> | |
` | |
}) | |
export class TodoItemRenderer{ | |
@Input() todo | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from 'angular2/core'; | |
import {TodoService} from './TodoService'; | |
import {TodoItemRenderer} from './TodoItemRenderer'; | |
import {SearchPipe} from './search-pipe'; | |
@Component({ | |
selector: 'todo-list', | |
pipes: [SearchPipe], | |
directives: [TodoItemRenderer], | |
template: ` | |
<ul> | |
<li *ngFor="#todo of todoService.todos | search"> | |
<todo-item-renderer [todo]="todo"></todo-item-renderer> | |
</li> | |
</ul> | |
` | |
}) | |
export class TodoList{ | |
constructor(public todoService: TodoService){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class TodoModule{ | |
status: string = "started"; | |
isEdit: boolean = false; | |
constructor(public title:string=""){ | |
} | |
toggle(){ | |
this.status = this.status === "started" ? | |
"completed": | |
"started"; | |
} | |
edit(){ | |
this.isEdit = !this.isEdit; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {TodoModule} from './TodoModule'; | |
export class TodoService{ | |
todos = [ | |
//init some todos | |
new TodoModule("eat"), | |
new TodoModule("sleep"), | |
new TodoModule("code") | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment