可以在代码中使用管道吗? [英] 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}}
而且效果很好.
是否可以在代码中使用管道?
Is it possible to use a pipe in the code?
我尝试这样使用它:
let name = `${user|userName}`;
但它显示
未定义用户名
userName is not defined
我的另一种方法是在代码中手动使用db.collection.findOne()
.但是有什么聪明的方法吗?
My alternative way is using db.collection.findOne()
manually in the code. But is there any smart way?
推荐答案
首先在模块的providers
中声明管道:
First declare the pipe in the providers
of your module:
import { YourPipeComponentName } from 'your_component_path';
@NgModule({
providers: [
YourPipeComponentName
]
})
export class YourServiceModule {
}
然后,您可以在类似这样的组件中使用@Pipe
:
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');
}
}
这篇关于可以在代码中使用管道吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文