Angular 内置了一些管道,比如 DatePipe、UpperCasePipe、LowerCasePipe、CurrencyPipe 和 PercentPipe。 它们全都可以直接用在任何模板中;但是在angular(angular2)中由于性能的原因官方没有提供有关筛选和排序的管道,于是当我们在使用*ngFor需要筛选数据时可以自定义筛选管道。

  • in component
filterargs = {title: 'hello'};
items = [{title: 'hello world'}, {title: 'hello kitty'}, {title: 'foo bar'}];
  • in template
<li *ngFor="let item of items | myfilter:filterargs">
  • in pipe
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'myfilter',
pure: false
})
export class MyFilterPipe implements PipeTransform {
transform(items: any[], filter: Object): any {
if (!items || !filter) {
return items;
}
// filter items array, items which match and return true will be
// kept, false will be filtered out
return items.filter(item => item.title.indexOf(filter.title) !== -1);
}
}
  • in app.module.ts; you no longer need to register the pipes in your @Component
import { MyFilterPipe } from './shared/pipes/my-filter.pipe';

@NgModule({
imports: [
..
],C
declarations: [
MyFilterPipe,
],
providers: [
..
],
bootstrap: [AppComponent]
})
export class AppModule { }

参考链接:

https://stackoverflow.com/questions/34164413/how-to-apply-filters-to-ngfor

在angular中自定义筛选管道的更多相关文章

  1. angular中自定义依赖注入的方法和decorator修饰

    自定义依赖注入的方法 1.factory('name',function () { return function(){ } }); 2.provider('name',function(){ thi ...

  2. C# Winform中自定义筛选及自带统计行的Datagridview控件

    网上分享有很多种自制DGV控件,都有不小的缺陷. 没办法,按需求自己定制了一个. 一.过滤方面类似于Excel的筛选功能.支持右键菜单筛选,同时也支持在文本框输入文字按焦点列进行筛选: 二.统计行我采 ...

  3. angular中transclude的理解

    今天被这个transclude搞糊涂了,弄了半天,才知道原来使用起来很简单.很烦恼为社么书中的对于这个的介绍这么晦涩难懂.直到看到了这篇文章,才让我弄清楚了. 一.transclude介绍 trans ...

  4. 在angular7中创建组件/自定义指令/管道

    在angular7中创建组件/自定义指令/管道 组件 使用命令创建组件 创建组件的命令:ng generate component 组件名 生成的组件组成: 组件名.html .组件名.ts.组件名. ...

  5. Angular中的内置指令和自定义指令

    NG中的指令,到底是什么(what)? 为什么会有(why)?以及怎样使用(how)? What: 在NG中,指令扩展HTML功能,为 DOM 元素调用方法.定义行为绑定数据等. Why: 最大程度减 ...

  6. 【响应式编程的思维艺术】 (5)Angular中Rxjs的应用示例

    目录 一. 划重点 二. Angular应用中的Http请求 三. 使用Rxjs构建Http请求结果的处理管道 3.1 基本示例 3.2 常见的操作符 四. 冷热Observable的两种典型场景 4 ...

  7. angular2+ 自定义pipe管道实例--定义全局管道及使用

    首先到项目目录下ng g pipe pipe/myslice 就会在app目录下生成一个pipe文件夹文件夹下有myslice.pipe.ts文件,如果没有也可以自己手动新建 然后需要再app.mod ...

  8. Angular中innerHTML标签的样式不起作用详解

    1.背景 在最近angular的项目中,需要用到[innerHTML]标签来指定一个div的样式: //HTML部分 <div class="contents" [inner ...

  9. Angular中ngCookies模块介绍

    1.Cookie介绍 Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短 ...

随机推荐

  1. Python批量重命名文件

    批量替换文件名中重复字符: # -*- coding: UTF-8 -*- import os path = raw_input("请输入文件夹路径:") oldname = ra ...

  2. i++ 和 ++i 的区别和实现

    ++i 和 i++ ++i 和 i++ 的区别 1)i++ 返回的是 i 的值,++i 返回的是 i+1 的值 2)i++ 不能用作左值,++i 可以用作左值 左值和右值的区别是什么? 根本区别是:能 ...

  3. latex学习笔记----基本知识、文档排版

    1.空格和制表符等空白字符视为相同的空白距离,多个连续的空白字符等同为一个字符. 2.#  $  %  ^  _    {   }  ~ 在这些字符前面加上反斜线,就可以在文本中得到它们. 反斜线\不 ...

  4. python3 subprocess 内存操作视频转换流格式

    import subprocessout = open('./tmp/sss.mp4','rb').read()p = subprocess.Popen(["./ffmpeg",& ...

  5. 一、Cookie和Session介绍

    会话跟踪 1. 什么是会话  * 用户拨打10086,从服务台接通后会话开始:  * 用户发出话费查询请求,服务台响应.这是该会话中的一个请求:  * 用户发出套餐变更请求,服务台响应.这是该会话中的 ...

  6. 14)PHP,系统错误

    E_ERROR:系统严重错误 一发生,程序立即停止执行. 该错误一般希望马上. E_WARNING:系统警告 一发生,提示错误,并继续执行. 通常该错误希望能够在“下一工作日”去处理掉(解决). E_ ...

  7. ios AVPlayer参考

    http://www.cnblogs.com/kenshincui/p/4186022.html#avPlayer

  8. android 获得存储设备状态

    1.获取存储器总大小,可用大小 File path= Environment.getExternalStorageDirectory();StatFs fs = new StatFs(path.get ...

  9. PCoA|NMDS|STRESS|RDA |RA|Unimodal|CCA|Generalized Joint Attribute Modeling

    PCoA:主坐标轴分析 数值型变量使用各种距离公式,而分类变量看是否相同,比如, Aabbcc || Aaffff 其中,两个相同,4个不同,一组6个,则(6+6-2*2)=8. PC0A与PCA区别 ...

  10. ios 获取app版本号

    let infoDictionary = Bundle.main.infoDictionary!let appversion = infoDictionary["CFBundleShortV ...