Created
September 18, 2018 16:32
-
-
Save huyinghuan/3df296e54179e826dbd7444916d61716 to your computer and use it in GitHub Desktop.
angular 5 input date component
This file contains hidden or 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
import { Component, Input, OnInit, EventEmitter } from '@angular/core'; | |
var template = ` | |
<input type="date" [ngModel]="now | date:'yyyy-MM-dd'" (ngModelChange)="change($event)"> | |
` | |
@Component({ | |
selector: 'date-picker', | |
template: template, | |
outputs: ['dateChange'] | |
}) | |
export class Datepicker implements OnInit{ | |
@Input() public now:Date; | |
public dateChange = new EventEmitter() | |
constructor(){} | |
ngOnInit(){ | |
if(!(this.now instanceof Date)){ | |
this.now = new Date() | |
} | |
} | |
change($event){ | |
let chooseDate = new Date($event) | |
this.now = chooseDate | |
this.dateChange.emit(chooseDate) | |
} | |
} |
This file contains hidden or 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
<date-picker [now]="defaultDate" (dateChange)="defaultDate = $event"></date-picker> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment