I am trying to build a datetime picker directive like the following.
<input [(ngModel)]="date1" datetime-picker date-only />
and date1
is assigned as a Date, e.g., new Date()
When I display this in html, text in input element looks like the following
Thu Jan 01 2015 00:00:00 GMT-0500
I want to display like the following instead
2015-01-01 00:00:00
I want to format date WITHIN a directive using DatePipe instead of showing result from default toString() function.
My question is; "within a directive, how do I access ngModel variable?", e.g., date1, so that I can add toString() method.
If my approach is not right, please advise me.
Here is simple and easy way to listen and notify ngModel. I have just demonstrated with jQuery for easy understanding. Practically, it can be anything.
For accessing
ngModel
, you can simply just have an@Input()
in yourdatetime-picker
. And since you are using 2-way data binding, you have to emit the changes you make tongModel
.Check this plunk
The better way IMHO, is using the
DatePipe
Check this plunk
You can use "keyup" and "this.ngModel.model"