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?
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]
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With