将组件变量绑定到管道过滤器 [英] Bind component variable to pipe filter
问题描述
将值传递到管道过滤器时出现问题。我需要从我的组件中传递一个名为pagex的变量形式的参数值,但我找不到使它正常工作的语法...或者我遗漏了一些东西。谢谢您的帮助。
i have a problem when passing values to my pipe filter. I need to pass an argument value in the form of a variable called pagex from my component and I cant find the syntax to make it work... or I'm missing something. Thanks for the help.
myComponent
export class ItemsComponent {
items:any[]
pagex:number;
constructor(private ItemService:ItemService){
this.ItemService.getItems()
.subscribe(items =>{
this.items=items;
this.pagex=2;
});
}
以下手动传递值的方法有效:
The following, passing the value manually, works:
<div *ngFor="let item of items| myfilter: '2'">
这并没有,已经尝试了很多组合...
and this doesnt, tried already many combinations...
<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'>
mypipe
@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));
}
}
}
推荐答案
您在过滤器输入的类型上犯了一个错误。
You have made mistake in the filter input's type.
过滤器的参数必须为数字类型而不是任何[]
The parameters to the filter must be of number type and not 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();
}
}
我不知道过滤器到底在做什么,并且没有实施服务。所以我尝试了这种方式。您可以在下面查看 punker
I dont know what is the filter doing exactly and there is no service implemented. So I tried this way. You can check the plunker below
这篇关于将组件变量绑定到管道过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!