Angular使用自定义管道

前端之家收集整理的这篇文章主要介绍了Angular使用自定义管道前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我很乐意帮助将内置管道导入角度为2的自定义管道.

这是我的代码

@Pipe({ name: 'tablePipe' })

export class TablePipe implements PipeTransform {
    constructor(private decimalPipe: DecimalPipe) {

    }
    transform(field: any,format: Format,formatArg: string): any {
        let formattedField: any = ''
        switch (format) {
            case 'number':
                {
                    formattedField = this.decimalPipe.transform(field,formatArg);
                    break;
                }
        }
        return formattedField;
    }
}

export type Format = 'date' | 'string' | 'number';

这是我得到的错误

EXCEPTION: Uncaught (in promise): Error: No provider for DecimalPipe!

在组件中导入常规自定义管道时,我使用:

@Component({
  ...,pipes: [MyCustomPipe],...
})

解决方法@H_301_31@
为此,您需要将DecimalPipe添加到某处的提供程序

例如

@NgModule({
  providers: [DecimalPipe],...
})
export class AppModule {}

您还可以将其添加到组件的提供者(您使用管道的组件或祖先组件).

猜你在找的Angularjs相关文章