[Angular 2] Using Pipes to Filter Data
2016-03-21 20:25
495 查看
Pipes allow you to change data inside of templates without having to worry about changing it in the Controller. Creating a custom Pipe is as simple as giving it a name and a transform function that output what you expect.
Create a Pipe:
Using a pipe:
-------------------
Create a Pipe:
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'); } ) } }
Using a pipe:
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> ` })
-------------------
相关文章推荐
- Angular Light 指令用法
- [Angular 2] Controlling how Styles are Shared with View Encapsulation
- [Angular 2]ng-class and Encapsulated Component Style2
- [Angular 2] Passing data to components with @Input
- angularJS provider/factory/service初解析
- 实践中学习AngularJS表单
- 为克服HTML在构建应用上的不足而设计! AngularJS简介!零玖玖教育
- AngularJS UI Router(ui.router)嵌套视图(Nested Views)
- ionic & AngularJS 传递url类型的参数
- Angular报错
- [Angular 2] Adding a data model
- [Angular 2] Using ng-model for two-way binding
- [Angular 2] Using ng-for to repeat template elements
- AngularJS学习笔记之一: AngularJS入门
- AngularJS学习笔记之二:开发、调试和测试工具
- [Angular 2] Using the @Inject decorator
- AngularJS中的指令实践开发指南(二)
- AngularJS 中的指令实践开发指南(一)
- 浅析AngularJS中的指令
- angularjs控制器之间通信,事件通知服务