stackoverflowとgoogleで何時間も検索した後、検索しているものが見つかりませんでしたが、別の解決策のアイデアを得ることができました。
オブジェクトの例:
items = [
{
title: "This is title",
email: "[email protected]",
status: "confirmed"
},
{
title: "This another one",
email: "[email protected]",
status: "pending"
{
title: "Just a random string",
email: "[email protected]",
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: "[email protected]",status: "confirmed"}
そのまま動作しますが、RegExpで動作させるつもりでした。試しましたが、varsomething = new RegExp(// some rule)を使用すると、何らかの理由でエラーが発生しました。
どんなアイデアでも大歓迎です。
これを試して
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>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加