如何注入管道中的存储?

问题描述

在当前情况下,我想使用管道来转换数据。 但是在该管道内部,我想访问商店以获得一些偏好。 可以在管道内注入存储吗?

解决方法

function yourProcessOrServiceMethod(input: YourType): string { ... }

@Pipe({ name: 'yourPipe' })
export class YourPipeDefinition implements PipeTransform {
  transform(value: YourType): string // it can be other than string
    return yourProcessOrServiceMethod(value);
  }
}
@NgModule({...,providers: [...,YourPipeDefinition] })
export class AnyModule { ... } // but recommend define in AppModule 
{{ compData | yourPipe }}

另一个示例:https://www.digitalocean.com/community/tutorials/angular-custom-pipes-angular