在stackoverflow上搜索了几个小时后谷歌我找不到我正在搜索的内容,我确实发现了一些让我对替代解决方案有所了解的东西。
对象示例:
items = [
{
title: "This is title",
email: "test@test.com",
status: "confirmed"
},
{
title: "This another one",
email: "someone@something.com",
status: "pending"
{
title: "Just a random string",
email: "me@you.co.uk",
status: "pending"
{
]
决议:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter',
pure: false
})
export class FilterPipe implements PipeTransform {
transform(value: any, args?: any): any {
if(args == '') { return value; }
let query = args.toLowerCase();
return value.filter(task =>
task.title.toLowerCase().indexOf(query) > -1 ||
task.email.toLowerCase().indexOf(query) > -1 ||
task.status.toLowerCase().indexOf(query) > -1
);
}
}
<div *ngFor="item of (items | filter:'this is') ">
{{item | json}}
</div>
这会给我:
{title: "This is title", email: "test@test.com",status: "confirmed"}
它的工作原理,但我的意图是使它与RegExp一起工作,我尝试但由于某种原因,当我使用var something = new RegExp(//某些规则)时出现错误。
非常感谢任何想法。
答案 0 :(得分:1)
试试这个
import {Pipe} from 'angular2/core';
// Tell Angular2 we're creating a Pipe with TypeScript decorators
@Pipe({
name: 'RegexPipe'
})
export class RegexPipe {
transform(value, args?) {
// ES6 array destructuring
let [pattern] = args;
return value.filter(task => {
reg = new Regex(pattern);
found = reg.test(task.title);
return found;
});
}
}
<div *ngFor="item of (items | RegexPipe:'/this is (.)*/') ">
{{item | json}}
</div>