将组件变量绑定到管道过滤器

Bind component variable to pipe filter

我在将值传递到管道过滤器时遇到问题。我需要从我的组件传递一个名为 pagex 的变量形式的参数值,但我找不到使其工作的语法......或者我遗漏了一些东西。感谢您的帮助。

我的组件

export class ItemsComponent { 
    items:any[]
    pagex:number;

constructor(private ItemService:ItemService){
    this.ItemService.getItems()
    .subscribe(items =>{
        this.items=items;
        this.pagex=2;
    });
}

以下手动传递值有效:

<div *ngFor="let item of items| myfilter: '2'">

这不行,已经尝试了很多组合...

<div *ngFor="let item of items| myfilter: {{pagex}}">
<div *ngFor="let item of items| myfilter: '{{pagex}}'">
<div *ngFor="let item of items| myfilter: {{pagex.toString()}}">
<div *ngFor="let item of items| myfilter: pagex>
<div *ngFor="let item of items| myfilter:'pagex'>

我的管道

@Pipe({
    name: 'myfilter',
})
export class MyFilterPipe implements PipeTransform {
    transform(items: any[], args: any[]): any {
        if(items!=undefined){

        console.log(args[0])
        var maxItems = 5;
           var start_index= (args[0]*maxItems)-maxItems-1;
           var end_index= args[0]*maxItems;
    return items.filter((item, index) => (index > start_index) && (index <end_index));
    }
}

}

编辑。试试这个,你好像错过了:' '

<div *ngFor="let item of items| myfilter: '{{pagex}}'">

您输入的过滤器类型有误。

过滤器的参数必须是数字类型,而不是 any[ ]

import{PipeTransform,Pipe} from '@angular/core';

@Pipe({
    name:'myFilter'
})

export class myFilter implements PipeTransform{

    transform(inputDate:string,integerValue:number):string{
      console.log(integerValue);

      return integerValue.toString();             
    }
}

我不知道过滤器到底在做什么,没有实现任何服务。所以我尝试了这种方式。你可以查看下面的plunker

LIVE DEMO