No provider for DatePipe

Viewed 53259

I tried to use DatePipe in my service like this:

import { Injectable } from '@angular/core';
import { DatePipe } from '@angular/common';
import { TranslateService } from '@ngx-translate/core';

@Injectable({
   providedIn: 'root',
})
export class BaseService {

    public items = [];
    constructor(
        protected datePipe: DatePipe,
        protected translateService: TranslateService
    ) { }
}

But I get this error: NullInjectorError: No provider for DatePipe!". What is the cause?

1 Answers

Just add the DatePipe in the NgModule providers section in your app.module.ts file.

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

...

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    DatePipe,
    ...
  ],
  bootstrap: [AppComponent]
})
Related