[英]How can I communicate between Pipe and Component in Angular 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
傳遞到Pipe的newVal
中。
// HTML模板
您可以使用模板中的管道並將新值傳遞給它。
<div>{{ yourValue | yourPipe: newChildData }}<div>
或者,由於Pipe只是Javascript類,因此您可以將其導入組件中並使用它。
const pipe = new youPipe();
const result = pipe.transform(yourValue, newChildData);
但我不建議稍后。
如果要以編程方式使用管道,請考慮將轉換邏輯拆分為服務,然后在pipe
和component
導入並使用該服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.