In AngularJS, I am able to use filters (pipes) inside of services and controllers using syntax similar to this:
$filter(\'date\')(myDate, \'yyyy-MM-dd\');
If you don't want do 'new myPipe()' because you're injecting dependencies to pipe, you can inject in component like provider and use without new.
Example:
// In your component...
import { Component, OnInit } from '@angular/core';
import { myPipe} from './pipes';
@Component({
selector: 'my-component',
template: '{{ data }}',
providers: [ myPipe ]
})
export class MyComponent() implements OnInit {
data = 'some data';
constructor(private myPipe: myPipe) {}
ngOnInit() {
this.data = this.myPipe.transform(this.data);
}
}
As of Angular 6 you can import formatDate
from @angular/common
utility to use inside the components.
It was intruduced at https://github.com/smdunn/angular/commit/3adeb0d96344c15201f7f1a0fae7e533a408e4ae
I can be used as:
import {formatDate} from '@angular/common';
formatDate(new Date(), 'd MMM yy HH:mm', 'en');
Although the locale has to be supplied