我有一个自定义的十进制格式管道,它使用Angular 十进制管道.此管道是共享模块的一部分.我将在功能模块中使用它,并且在运行应用程序时不会出现提供程序错误.

Please ignore if there are any typo

./src/pipes/custom.pipe.ts

import { DecimalPipe } from '@angular/common';
..
@Pipe({
    name: 'customDecimalPipe'
})
...
export class CustomPipe {
constructor(public decimalPipe: DecimalPipe) {}
transform(value: any, format: any) {
    ...
}

./modules/shared.module.ts

import  { CustomPipe } from '../pipes/custom.pipe';

...

@NgModule({
  imports:      [ .. ],
  declarations: [ CustomPipe ],
  exports:    [ CustomPipe ]
})
export class SharedModule { }

我将自定义管道注入其中一个组件中,并调用transform方法来获得转换后的值.共享模块导入到功能模块中.

推荐答案

如果要在组件中使用pipe的transform()方法,还需要将CustomPipe添加到模块的提供程序中:

import  { CustomPipe } from '../pipes/custom.pipe';

...

@NgModule({
  imports:      [ .. ],
  declarations: [ CustomPipe ],
  exports:    [ CustomPipe ],
  providers:    [ CustomPipe ]
})
export class SharedModule { }

Angular相关问答推荐

当try 使用Angular和PrimeNg手动聚焦输入时,我做错了什么?

CDK虚拟滚动由于组件具有注入属性而在滚动时看到错误的值

如何在滑动滑块中获取当前项目的索引?

图像未显示在视图屏幕上-Angular 投影

带信号数据源的17角material 表

Angular 16独立依赖注入问题

当在server.ts文件中定义API路径时,Angular 17构建失败

等待可观察性完成后再调用下一个

Angular MSAL - 对 Bearer 令牌和 AUD 属性的怀疑

Moment.js 和 Angular 库

Angular:如何设置 PrimeNG p-steps 组件中已完成步骤的样式?

Angular 获取视频持续时间并存储在外部变量中

在 Angular material 表单元格中渲染 html

如何设置Angular 4背景图片?

Angular/RxJS 6:如何防止重复的 HTTP 请求?

Angular 5 中服务的生命周期是什么

Angular 2如何让子组件等待异步数据准备好

Angular2 Base64 清理不安全的 URL 值

Angular 5 中 value 和 ngValue 的区别

ngClass 中的多个条件 - Angular 4