How to use a pipe in a component in Angular 2?

后端 未结 4 1306
终归单人心
终归单人心 2021-01-12 05:22

I have a pipe class which returns data based on the arguments you are passing. I know how to use it in my template HTML using the | symbol, but I want to use it

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 06:00

    You can call your pipe directly in your code by using:

    YourPipeClass.prototype.transform(value, arg1, arg2);
    

    You can call it from inside your component or from anywhere else that imports it.

    There is also the new way:

    new SortTodosPipe().transform(value, arg1, arg2);
    

    But keep in mind it will create an object, so either save that object for later use or use the prototype method.

    Anyway you choose, you must add the pipe to your providers if you use it inside a component, like so:

    @NgModule({
        providers: [YourPipe]
    })
    

提交回复
热议问题