微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数组 – 以angle2过滤数组

我正在研究如何在Angular2中过滤数据数组。

我研究了使用自定义管道,但我觉得这不是我正在寻找的,因为它似乎更适合简单的演示文稿转换,而不是过滤大量的数据。

阵列如下所示:

getLogs(): Array<Logs> {
        return [
            { id: '1',plate: 'plate1',time: 20 },{ id: '1',plate: 'plate2',time: 30 },plate: 'plate3',{ id: '2',plate: 'plate4',plate: 'plate5',plate: 'plate6',time: 30 }
        ];
    }

我想通过id过滤这个。所以当我在搜索栏中输入“1”时,它会更新以显示相应的值。

如果有一个如何做到这一点的方法,我很想知道!

没有办法使用认管道。以下是支持的管道列表: https://github.com/angular/angular/blob/master/modules/angular2/src/common/pipes/common_pipes.ts

那就是说你可以轻松地添加一个这样的用例的管道:

import {Injectable,Pipe} from 'angular2/core';

@Pipe({
    name: 'myfilter'
})
@Injectable()
export class MyFilterPipe implements PipeTransform {
    transform(items: any[],args: any[]): any {
        return items.filter(item => item.id.indexOf(args[0]) !== -1);
    }
}

并使用它:

import { MyFilterPipe } from './filter-pipe';
(...)

@Component({
  selector: 'my-component',pipes: [ MyFilterPipe ],template: `
    <ul>
      <li *ngFor="#element of (elements | myfilter:'123')">(...)</li>
    </ul>
  `
})

希望它能帮助你,蒂埃里

原文地址:https://www.jb51.cc/angularjs/144075.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐