[英]using ngModelChange it does not change date when user selects the date first time using date picker
I am using two date picker From date and To date , actually what i want is that when user picks a date (From date picker) and that date is greater then the value of (To date picker) its should then assign the (From date picker) value to (To date picker) as well and vice versa. 我正在使用两个日期选择器From date和To date,实际上我想要的是当用户选择一个日期(From date picker)并且该日期大于然后(To date picker)的值时,它应该分配(From date)选择器)的值也设置为(日期选择器),反之亦然。
I am using the following code but its not working properly 我正在使用以下代码,但无法正常工作
Component File 组件文件
if (callFrom == 'from' && claimCharge.Dos_From) {
if (this.getLengthOfYear(claimCharge.Dos_To)) {
if (this.checkIfTodayDate(claimCharge.Dos_To)) {
if (this.getLengthOfDate(claimCharge.Dos_From)) {
.Dos_To = claimCharge.Dos_From;
}
}
}
}
if (callFrom == 'from' && claimCharge.Dos_From) {
if (claimCharge.Dos_From >= claimCharge.Dos_To) {
if (this.getLengthOfYear(claimCharge.Dos_From)) {
if (claimCharge.Dos_To.toString() != new Date(new Date().setHours(0, 0, 0, 0)).toString() as any) {
claimCharge.Dos_To = claimCharge.Dos_From;
}
}
}
}
HTML : HTML:
<datetime [attr.id]="'txtProcedureDOSTo_'+ndx" [timepicker]="false" [datepicker]="datepickerOpts" (ngModelChange)="methodFromAndToCheck(p,'to')" [(ngModel)]="p.Dos_To" ></datetime>
<datetime [attr.id]="'txtProcedureDOSFrom_'+ndx" [timepicker]="false" [datepicker]="datepickerOpts" (ngModelChange)="methodFromAndToCheck(p,'from')" [(ngModel)]="p.Dos_From"></datetime>
Hopefully I have understood you correctly, that is my solution: 希望我正确理解了您,这是我的解决方案:
HTML: HTML:
<input placeholder="From" [(ngModel)]="from" type="date" (change)="onChange('from')">
<input placeholder="To" [(ngModel)]="to" type="date" (change)="onChange('to')">
TS Component: TS组件:
import {Component, OnInit} from '@angular/core'
@Component({
selector: 'date',
templateUrl: './date.component.html',
styleUrls: ['./date.component.css']
})
export class DateComponent implements OnInit {
from: string;
to: string;
ngOnInit(){
// get current date to set the starting values for inputs
// d is a type of Date()
let d = new Date();
// d.toISOString() returns date in format YYYY-MM-DDTHH:mm:ss.sssZ
// since input of "date" type uses YYYY-MM-DD format
// we take first 10 characters with .slice(0,10)
this.from = d.toISOString().slice(0,10);
this.to = d.toISOString().slice(0,10);
}
onChange(input: string){
if (input === 'from') {
// if Date from input "to" is less than from input "from"
// value of the input "to" is equal to value of the input "from"
this.to = new Date(this.to) < new Date(this.from) ? this.to = this.from : this.to;
} else {
// if Date from input "to" is less than from input "from"
// value of the input "from" is equal to value of the input "to"
this.from = new Date(this.to) < new Date(this.from) ? this.from = this.to : this.from;
}
}
}
Let me know if it worked for you as well. 让我知道它是否对您也有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.