Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pipe in ts not HTML

I adding text into html element from ts

like this

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

this will create html like

<text>Data will come here</text> 

I want to use pipe to convert this data into some form how can I do that from ts code ?

and as I am creating this HTML dynamically I cannot use pipe like this

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

I am looking for somethig like

this.legend.append('text')   .attr('x', legendRectSize + legendSpacing)   .attr('y', legendRectSize - legendSpacing)   .text(function(d) { return d; }).applypipe('pipename'); 
like image 684
Arun Tyagi Avatar asked Sep 23 '16 06:09

Arun Tyagi


People also ask

Can I use pipe in TS file?

use date pipe in component ts files In laterst version of Angular i.e., from version 6 we can directly use angular pipes's public methods inside the components to format the values. For example we use date pipe format method formatMethod in component file by importing it from the @angular/common as shown below.

How do you call a pipe from a component?

If you want to call the pipe directly within the component class, you need to instantiate it and call its tranform method: @Component({ (...) })


2 Answers

First import your pipe in component. And then use your pipe in your component. Like this..

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 (Use in your typescript code)

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

xyz.html (Use in your html file)

<ul>     <li *ngFor="todo for todos | filter:'txtsearch'"> {{todo.name}} </li> </ul> 
like image 125
Bharat Chauhan Avatar answered Sep 23 '22 00:09

Bharat Chauhan


If Pipename is your custom pipe then if you want to use the same in your ts file then you can use below code

import {Pipename} from './pipename';  Pipename.prototype.transform(arguments);//this is how u can use your custom pipe 
like image 35
Sudheer KB Avatar answered Sep 24 '22 00:09

Sudheer KB