标签: angular2-custom-pipes

Angular 2 - 如何在自定义管道内使用内置管道

我想使用内置货币管道制作自定义货币管道。我想要使​​用的方式是{{ anyNumber | customCurrency }}.然后在我的自定义货币管道中,我想使用内置货币管道。我可以根据某些逻辑决定货币管道的参数,并且不想像{{anyNumber | currency:'USD':false:'1:0-0'}}.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'customCurrency'
})
export class CustomCurrencyPipe implements PipeTransform {

  transform(value: any, args?: any): any {
      const defaultLocale = 'USD';
      const showSymbol = false;

      ......some logic here......      

      //HOW TO USE CURRENCY PIPE HERE?

 }

}
Run Code Online (Sandbox Code Playgroud)

angular-pipe angular2-custom-pipes angular

5
推荐指数
1
解决办法
1万
查看次数

找不到角度自定义管道

在我的应用程序中,我需要一个全局自定义管道,我尝试按照角度管道实现它, 但我总是看到这个错误

模板解析错误:找不到管道'formatdate'

formatdate.pipe

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'formatdate'
})

export class FormatdatePipe implements PipeTransform {

  transform(dateJson: any, args?: any): any {
.
 //code...
.
      return dateJson;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

app.module

import { FormatdatePipe } from './shared/pipes/formatdate.pipe';
@NgModule({
  declarations: [
    AppComponent, FormatdatePipe 
  ],
Run Code Online (Sandbox Code Playgroud)

如果我在我的所有模块中导入它而不是在主app.module中导入此管道,我是否需要例程管道模块或其他东西

angular2-custom-pipes angular

4
推荐指数
1
解决办法
4407
查看次数

自定义管道实例在哪里?组件代码如何访问在其 HTML 中使用的自定义管道实例?

我在组件的 HTML 部分使用了一个自定义管道。它在模块中声明:

  declarations: [  I18nPipe ],
Run Code Online (Sandbox Code Playgroud)

我希望能够从组件代码(而不是transform方法)调用它的方法。

我希望管道实例位于依赖注入上下文中的某个地方,以便我可以抓住它。但是我错了。如果我将它注入到组件的构造函数中(例如,像任何普通服务一样):

  constructor(private i18nPipe: I18nPipe)  
Run Code Online (Sandbox Code Playgroud)

然后我得到一个错误:没有提供者。所以我将它包含在providers同一模块的部分中:

  providers: [ I18nPipe ]
Run Code Online (Sandbox Code Playgroud)

然后我将可以在组件代码中访问它,但是我的自定义管道会有两个实例。

  1. 创建者providers,在 DI 上下文中可用。在构造函数中注入时,我将获得此实例,因此我将在我的组件代码中使用此实例。

  2. 在 HTML 中使用的实例。它在哪里生活?我想在我的组件代码中访问这个实例,而不是“提供的”一个;我怎样才能获得它?

angular2-di angular2-custom-pipes angular

2
推荐指数
1
解决办法
1355
查看次数

定制管| 用于计算 angular2 中的相对时间的过滤器

在学习过程中,我遇到了自定义管道的创建,所以我认为这会有所帮助。

angular2-pipe angular2-custom-pipes angular

1
推荐指数
1
解决办法
1808
查看次数