是否可以在Angular 2的代码中使用管道?

时间:2022-03-24 12:50:59

When I use my custom pipe in template, it is like this. And it works well.

当我在模板中使用自定义管道时,就像这样。它运作良好。

{{user|userName}}

Is it possible to use pipe in the code? I try to use like this,

是否可以在代码中使用管道?我尝试这样使用,

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

But it shows

但它表明

userName is not defined

userName未定义

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

我的另一种方法是在代码中手动使用db.collection.findOne()。但有什么聪明的方法吗?

2 个解决方案

#1


65  

@Siva is correct. And thanks!

@Siva是对的。谢谢!

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

因此在组件中使用管道的方式如下:

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

Link to another similar question.

链接到另一个类似的问题。

#2


41  

this is the way you can use Pipe in component:

这是在组件中使用Pipe的方法:

import { YourPipeComponentName } from '@angular/common';

class YourService {

  constructor(private pipe: YourPipeComponentName) {}

  YourFunction(value) {
    this.pipe.transform(value, 'pipeFilter');
  }
}

#1


65  

@Siva is correct. And thanks!

@Siva是对的。谢谢!

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

因此在组件中使用管道的方式如下:

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

Link to another similar question.

链接到另一个类似的问题。

#2


41  

this is the way you can use Pipe in component:

这是在组件中使用Pipe的方法:

import { YourPipeComponentName } from '@angular/common';

class YourService {

  constructor(private pipe: YourPipeComponentName) {}

  YourFunction(value) {
    this.pipe.transform(value, 'pipeFilter');
  }
}