How to use pipe in ts not HTML

前端 未结 4 965
清歌不尽
清歌不尽 2021-02-02 12:33

I adding text into html element from ts

like this

this.legend.append(\'text\')
  .attr(\'x\', legendRectSize + legendSpacing)
  .attr(\'y\', legendRectSi         


        
相关标签:
4条回答
  • 2021-02-02 12:43

    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>
    
    0 讨论(0)
  • 2021-02-02 12:43

    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
    
    0 讨论(0)
  • 2021-02-02 12:47

    import pipe in the component

    import { PipeName } from './pipename'
    

    include it in the provides

    @Component({
        selector: 'pipe-using-component',
        templateUrl: './pipe-using-component.html',
        providers: [
            PipeName
        ],
    })
    

    inject it in the constructor

    export class PipeUsingComponent {
      constructor(private pipeName: PipeName)
       }
    
       var requiredResult = this.pipeName.transform(passvalue);
    }
    
    0 讨论(0)
  • 2021-02-02 12:49

    In your .ts

    import {YourPipe} from '/pipePath';
    
    
    let value = new YourPipe().transform(param);
    
    0 讨论(0)
提交回复
热议问题