2016-12-30 89 views
0

我怎样才能在角2

<tr *ngFor="let logDetails of logDetails | search : term" >

管和组件之间的通信,我有,我想用一个变量的管道。在我的组件中定义了相同的变量。我如何将这个值从Component传递给Pipe?

//Pipe Structure 

transform(value, [term], newVal) { 
    if (value == undefined && newVal == undefined) { 
    return undefined; 
    } else { 
    return value.filter((item) => item.LogString.startsWith(term)); 
    } 
} 

//Component Structure 

newVal(a) { 
    this.newChildData = a; 
    console.log(this.newChildData); 
} 

我想通过组件的newChildData到管newVal

// HTML模板

+0

当您调用管道时,通过模板将其传入。参数包含在文档中:https://angular.io/docs/ts/latest/guide/pipes.html – jonrsharpe

+0

谢谢jonrsharpe。但我不是通过模板发送newVal。有一些“术语”字段,我正在从文本框中发送。 – user3856563

+0

是的,我说你需要发送它需要的其他参数! RTFM。 – jonrsharpe

回答

0

这是不是你用管的模板,并通过新的价值到它。

<div>{{ yourValue | yourPipe: newChildData }}<div> 

或者,由于Pipe只是一个Javascript类,因此您可以将它导入到组件中并使用它。

const pipe = new youPipe(); 
const result = pipe.transform(yourValue, newChildData); 

但我不会建议以后。

如果要以编程方式使用管道,请考虑将变换逻辑分解为服务,然后在pipecomponent中导入并使用该服务。