12

I am trying to instantiate a DatePipe object in my Angular2 app to use transform(...) function in a component I'm developing.

// ...
import { DatePipe } from '@angular/common';

@Component({...})
export class PanelComponent implements OnInit {
    // ...
    datePipe: DatePipe = new DatePipe(); // Error thrown here
    // ...
}

This code segment worked fine in RC5. Now I am trying to upgrade to Angular2 final release and getting this error when I run ng serve or ng build,

~/tmp/broccoli_type_script_compiler-input_base_path-XitPWaey.tmp/0/src/app/panel/panel.component.ts (33, 24): 
Supplied parameters do not match any signature of call target.

How can I resolve this issue? Is there another way of instantiating a Pipe? Or has Angular stopped supporting instantiating of Pipes inside components?

  • 1
    You are just using the pipe in the wrong way. You should not manually instantiate a pipe, use DI instead. – Harry Ninh Sep 21 '16 at 04:33

2 Answers2

30

If you take a look at source code then you will see that DatePipe constructor asks for a required parameter:

constructor(@Inject(LOCALE_ID) private _locale: string) {}

There is no default locale for DataPipe

https://github.com/angular/angular/blob/2.0.0/modules/%40angular/common/src/pipes/date_pipe.ts#L97

That's why typescript gives the error. This way you have to initiate your variable as shown below:

datePipeEn: DatePipe = new DatePipe('en-US')
datePipeFr: DatePipe = new DatePipe('fr-FR')
constructor() {
  console.log(this.datePipeEn.transform(new Date(), 'dd MMMM')); // 21 September
  console.log(this.datePipeFr.transform(new Date(), 'dd MMMM')); // 21 septembre
}

Hope it helps you!

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • 1
    Thank you. This resolves my issue. Apparently, this new constructor is introduced in Angular2 RC6. I have missed that since I was upgrading from RC5 to Final Release.. Pull request - [fix(i18n): Currency/Date/Number pipe use injected locale](https://github.com/angular/angular/commit/0a053a4cd52312e03179a3dfb5b6903aaa5a5a2e) – charith.arumapperuma Sep 21 '16 at 07:07
0

looks everything fine, error must be anywhere else in your code. See my plunker: https://plnkr.co/edit/koDu6YmB131E6sXc6rKg?p=preview

import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {DatePipe} from '@angular/common';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {

  dPipe = new DatePipe();

  constructor() {
    this.name = 'Angular2'
    console.dir(this.dPipe);
    console.log(this.dPipe.transform(new Date(), 'dd.MM'));
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

And @Harry Ninh .. you can't inject Pipes!!

slaesh
  • 16,659
  • 6
  • 50
  • 52
  • I believe plunker still uses an older version of Angular2. Following @yurzui's answer I did check DatePipe class history and found that new constructor is introduced in RC6 by this pull request. [fix(i18n): Currency/Date/Number pipe use injected locale](https://github.com/angular/angular/pull/11093) – charith.arumapperuma Sep 21 '16 at 07:03
  • As [this answer](https://stackoverflow.com/a/35152297/1575421) indicates, you can inject Angular pipes including DatePipe. I created a [Plunkr example](http://plnkr.co/edit/ZtI8sGJN5OCJgcv5ijFb?p=preview) of dependency injection with the DatePipe for reference. – jmmygoggle Mar 15 '18 at 02:11