当我在模板中使用自定义管道时,它是这样的:
{{user|userName}}
而且效果很好.
是否可以在代码中使用管道?
我试着这样使用它:
let name = `${user|userName}`;
但它表明
未定义用户名
我的替代方法是在代码中手动使用db.collection.findOne()
.但有什么聪明的方法吗?
当我在模板中使用自定义管道时,它是这样的:
{{user|userName}}
而且效果很好.
是否可以在代码中使用管道?
我试着这样使用它:
let name = `${user|userName}`;
但它表明
未定义用户名
我的替代方法是在代码中手动使用db.collection.findOne()
.但有什么聪明的方法吗?
首先声明模块providers
中的管道:
import { YourPipeComponentName } from 'your_component_path';
@NgModule({
providers: [
YourPipeComponentName
]
})
export class YourServiceModule {
}
然后,您可以在如下所示的组件中使用@Pipe
:
import { YourPipeComponentName } from 'your_component_path';
class YourService {
constructor(private pipe: YourPipeComponentName) {}
YourFunction(value) {
this.pipe.transform(value, 'pipeFilter');
}
}