I have tried many date picker in my angular2 app but none of them is working.Although the date picker is displaying on the view but the value of selected date is not getting
Angular 2 (Typescript)
can also do this with blur
event.
look closely at (blur)="newProjectModel.eEDate = eEDatePicker.value"
on blur event it assigns input temporary variable #eEDatePicker
value to our model variable newProjectModel.eEDate
<form (ngSubmit)="onSubmit()" #newProjectForm="ngForm" >
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="end-date">Expected Finish Date</label>
<div class='input-group date datetimepicker'>
<input type='text' class="form-control" required [(ngModel)]="newProjectModel.eEDate" name="eEDate" #eEDate="ngModel" #eEDatePicker (blur)="newProjectModel.eEDate = eEDatePicker.value" />
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
<div [hidden]="eEDate.valid || eEDate.pristine" class="alert alert-danger">
Expected Finish Date is required
</div>
</div>
</div>
</div>
<div class="modal-footer" [hidden]="submitted">
<button [hidden]="submitted" type="button" class="btn btn-red" data-dismiss="modal" >Cancel</button>
<button [hidden]="submitted" type="submit" class="btn btn-green" [disabled]="!newProjectForm.form.valid">Save</button>
</div>
<div class="modal-footer" [hidden]="!submitted">
<button [hidden]="!submitted" focus class="btn btn-green" data-dismiss="modal" (click)="submitted=false">Ok</button>
</div>
</form>
I'am using these bootstrap libraries:
bootstrap-datetimepicker.min.css
bootstrap-datetimepicker.min.js
This is my solution:
<input id="send_date"
type="text"
[(ngModel)]="this.dataModel.send_date"
name="send_date"
#send_date="ngModel"
class="date-picker"
data-date-format="dd-mm-yyyy">
//My data model has "send_date" property
dataModel: MyDataModel;
ngAfterViewInit() {
//important point: You have to create a reference to this outer scope
var that = this;
$('#send_date').datepicker({
//... your datepicker attributes
}).change(function () {
that.dataModel.send_date = $('#send_date').val();
});
}
I just found mydatepicker package today just type in npm install mydatepicker --save
and follow the instructions here if you don't like their naming style, just create a wrapper component around it that works with Date type
consider using angular-datepicker, it's a highly configurable date picker built for Angular applications.
Not sure if it helps but we wanted to develop a custom datepicker for our project and we couldn't find a lot of Angular2 Datepicker's at that point and hence ended up writing a simple one myself.
Its a simple one which selects the date and you can also specify dates that you want to disable before and after. It uses event emitter and sets the selected date in a text field. It is published in npm and I am working on improving it as well.
https://www.npmjs.com/package/angular2-simple-datepicker
Hope it helps.
In fact, you can use a datepicker by simply adding the date
value into the type
attribute of your inputs:
<input type="date" [(ngModel)]="company.birthdate"/>
In some browsers like Chrome and Microsoft Edge (not in Firefox), you can click on the icon within the input to display the date picker. Icons appear only when you mouse is over the input.
To have something cross browsers, you should consider to use Angular2 compliant libraries like: