Created
February 1, 2016 18:29
-
-
Save markdrake/d5158e3f57f7bc6f7ee8 to your computer and use it in GitHub Desktop.
How to set/get values from a date picker in jquery using angular 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path="../../../../typings/jquery/jquery.d.ts" /> | |
// TODO: Create date picker directive | |
declare var jQuery: JQueryStatic; | |
export class Main implements OnInit { | |
public date: any; | |
constructor() { | |
this.date = '2016-04-29'; | |
} | |
public ngOnInit() { | |
this.initJQueryComponents(); | |
} | |
public submit() { | |
console.log(jQuery('.datepicker').val()); | |
console.log(this.date); // Date picker value and this.date is the same | |
} | |
protected initJQueryComponents() { | |
jQuery(document).ready(() => { | |
let datePicker = jQuery('.datepicker').pickadate({ | |
selectMonths: true, // Creates a dropdown to control month | |
selectYears: 15 // Creates a dropdown of 15 years to control year | |
}) | |
.on('change', () => { | |
this.date = jQuery('.datepicker').val(); | |
console.log('triggered change'); | |
}); | |
datePicker.pickadate('picker').set('select', this.date, { format: 'yyyy-mm-dd'}); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="main"> | |
<input type="date" class="datepicker"> | |
<button (click)="submit()">Submit</button> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment