2016-09-23 27 views
3

我將文字轉換成HTML元素從TS如何使用管道在TS不是HTML

這樣

this.legend.append('text') 
    .attr('x', legendRectSize + legendSpacing) 
    .attr('y', legendRectSize - legendSpacing) 
    .text(function(d) { return d; }); 

這個喜歡

<text>Data will come here</text> 

將創建HTML我想用管將這些數據轉換成某種形式 我該如何從ts代碼中做到這一點?

,併爲我創建這個HTML動態我不能用管這樣

<text>{{Data will come here | pipename}} </text> 

我找somethig像

this.legend.append('text') 
    .attr('x', legendRectSize + legendSpacing) 
    .attr('y', legendRectSize - legendSpacing) 
    .text(function(d) { return d; }).applypipe('pipename'); 
+0

請澄清你的問題。我很難理解你在尋找什麼。 – MorKadosh

+0

現在好嗎? @MorKadosh –

+0

請添加完整的打字稿代碼。 –

回答

3

如果Pipename是您的自定義管道,然後,如果你想使用在你的ts文件中是一樣的,那麼你可以使用下面的代碼

import {Pipename} from './pipename'; 

Pipename.prototype.transform(arguments);//this is how u can use your custom pipe 
+0

非常感謝你!有用! – mondayguy

6

首先在c中導入你的管道omponent。然後在你的組件中使用你的管道。這樣的..

pipe.ts

/** 
* filter checkbox list 
*/ 
@Pipe({ name: 'filter', pure: true }) 
export class FilterPipe{ 
    transform(items: any[], args: any): any { 
    let filter = args.toString(); 
    if(filter !== undefined && filter.length !== null){ 
     if(filter.length === 0 || items.length ===0){ 
      return items; 
     }else{ 
      return filter ? items.filter(item=> item.title.toLocaleLowerCase().indexOf(filter) != -1) : items; 
     } 
    } 
    } 
} 

component.ts(在你的打字稿代碼使用)

filterPipe = new FilterPipe(); 
fiteredArr = filterPipe.transform(chkArray,txtSearch); 

xyz.html(HTML文件中使用)

<ul> 
    <li *ngFor="todo for todos | filter:'txtsearch'"> {{todo.name}} </li> 
</ul> 
+0

我的問題是diffrenet如何在javascript或打字稿代碼中使用此管道,但不能在html中使用 –

+0

我的回答也是在打字稿中使用管道..不是,在HTML –

+0

中使用'pure:true'爲默認...(https:/ /angular.io/guide/pipes#pure-and-impure-pipes) – Guntram