Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a pipe in the code?

When I use my custom pipe in a template, it is like this:

{{user|userName}}

And it works well.

Is it possible to use a pipe in the code?

I try to use it like this:

let name = `${user|userName}`;

But it shows

userName is not defined

My alternative way is using db.collection.findOne() manually in the code. But is there any smart way?

like image 570
Hongbo Miao Avatar asked Feb 02 '16 16:02

Hongbo Miao


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.

What is a pipe in code?

In computer programming, especially in UNIX operating systems, a pipe is a technique for passing information from one program process to another. Unlike other forms of interprocess communication (IPC), a pipe is one-way communication only.

Can we use pipe in component?

You don't have to duplicate your code if you want to also use a pipe's functionality in a component class. All you have to do really is inject the pipe like a service, and then call its transform method. The following works for any Angular 2+ app.


2 Answers

First declare the pipe in the providers of your module:

import { YourPipeComponentName } from 'your_component_path';  @NgModule({   providers: [     YourPipeComponentName   ] }) export class YourServiceModule { } 

Then you can use @Pipe in a component like this:

import { YourPipeComponentName } from 'your_component_path';  class YourService {    constructor(private pipe: YourPipeComponentName) {}    YourFunction(value) {     this.pipe.transform(value, 'pipeFilter');   } } 
like image 124
saghar.fadaei Avatar answered Sep 18 '22 17:09

saghar.fadaei


@Siva is correct. And thanks!

So the way to use the pipe in the component is like this:

let name = new UserNamePipe().transform(user); 

Link to another similar question.

like image 27
Hongbo Miao Avatar answered Sep 22 '22 17:09

Hongbo Miao