zoukankan      html  css  js  c++  java
  • [Angular 2] Using Pipes to Filter Data

    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.

    startsWith.ts:

    import {Pipe} from 'angular2/angular2';
    
    @Pipe({
        name: 'startsWith'
    })
    
    export class StartsWith{
    
        transform(value, [field, letter]){  // 1: value passed in, 2: array
            return value.filter((item) => {
                return item[field].startsWith(letter);
            })
        }
    }

    todoList:

    /**
     * Created by wanzhen on 23.10.2015.
     */
    import {Component, View, NgFor} from 'angular2/angular2';
    import {TodoService} from './todoService';
    import {TodoItemRender} from './todoItemRender';
    import {StartsWith} from './startsWith';
    
    @Component({
        selector: 'todo-list'
    })
    @View({
        pipes: [StartsWith],
        directives: [NgFor, TodoItemRender],
        template: `
              <div>
                    <todo-item-render
                        *ng-for="#todo of todoService.todos | startsWith:'title':'e'"  // title is the prop of #todo, filter get only start letter with 'e'
                        [todoinput]="todo"
                    >
                    </todo-item-render>
              </div>
        `
    })
    
    export class TodoList{
        constructor(
            public todoService:TodoService
        ){
    
        }
    }
  • 相关阅读:
    html之marquee详解
    委托delegate
    sql server 循环
    数据库可疑
    WP8数据存储--独立存储文件
    WP8数据存储--独立存储设置
    jQuery Mobile 自定义导航条图标
    JQuery Mobile 图片布局
    自定义jQuery Mobile工具栏按钮
    css透明度的设置 (兼容所有浏览器)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4916192.html
Copyright © 2011-2022 走看看