Using Angular 2, I have a parameter called "example" that is a Date object. In the template I want to format "example" with a date pipe. Something like this:
<input type="text" [value]="example | date:'shortTime'">
// 9:00 AM
However, I need to use Angular's reactive forms. FormControls take precedent, so formControlName
will override anything in the value
directive.
<input type="text" formControlName="example">
// Wed Mar 08 2017 09:00:00 GMT-0700 (MST)
<input type="text" formControlName="example" [value]="example | date:'shortTime'">
// Wed Mar 08 2017 09:00:00 GMT-0700 (MST)
The formControlName
directive will not accept pipes. How can I format a Date object in the template of a reactive form?
Pseudo-code:
@Component({
template: `
<form [formGroup]="formGroup">
Example: <input type="text" formControlName="example">
</form>
`
})
export class ExampleComponent implements OnInit {
public formGroup: FormGroup;
public example: Date = new Date();
public ngOnInit() {
this.formGroup = new FormGroup({
example: new FormControl(this.example)
});
}
}