I would like to override the "date" pipe and enjoy the benefit of global access everywhere just like the built-in pipe--aka, avoid having to import and use pipes[] array in every component annotation. Is this possible?
We can chain multiple pipes together.
Angular 7 provides some built-in pipes: Lowercasepipe. Uppercasepipe. Datepipe.
By default, pipes are defined as pure so that Angular executes the pipe only when it detects a pure change to the input value. A pure change is either a change to a primitive input value (such as String , Number , Boolean , or Symbol ), or a changed object reference (such as Date , Array , Function , or Object ).
Yes, you can use PLATFORM_PIPES
to add a custom pipe and name that pipe date
to hijack it.
@Pipe({
name : 'date' // Hijacks the 'date' pipe
})
class CustomDatePipe {
transform(val, args) {
return /* do something new with the value */;
}
}
@Component({
selector: 'my-app',
template : '{{mydate | date}}',
})
export class App {
mydate = Date.now();
}
// Provides the CustomDatePipe globally
bootstrap(App, [provide(PLATFORM_PIPES, {useValue: [CustomDatePipe], multi: true})]);
This way you won't have to add specify it every time in pipes
property in your components.
Here's a plnkr with an example working.
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