首页 > 其他 > 详细

[Angular 2] Pipe Purity

时间:2015-11-01 06:51:34      阅读:277      评论:0      收藏:0      [点我收藏+]

Explaining how Pipes only change by default when your Pipe input parameters change and not when your data changes. It also shows you how to make an “unpure” pipe if you always want your pipe to update.

 

import {Pipe} from ‘angular2/angular2‘;

@Pipe({
    name: ‘startsWith‘
})

export class StartsWith{

    transform(value, [field, letter]){
        return value.filter((item) => {
            return item[field].startsWith(letter);
        })
    }
}

Current Pipe only watch for [field, letter] changes, not value changes.

 

The way to tell pipe also watch for value changes is just add ‘pure: false‘:

import {Pipe} from ‘angular2/angular2‘;

@Pipe({
    name: ‘startsWith‘,
    pure: false
})

export class StartsWith{

    transform(value, [field, letter]){
        return value.filter((item) => {
            return item[field].startsWith(letter);
        })
    }
}

 

[Angular 2] Pipe Purity

原文:http://www.cnblogs.com/Answer1215/p/4926645.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!