I have two time fields but I can't get their values to compare them. All I get is undefined! I want to get the value as soon as the field is left (focusout).
Here is my .ts code:
isMeridian = false;
showSpinners = false;
timeStart: any;
timeSegment: any;
onKeyTime(event) {
console.log("onKeyTime :" + this.mySummaryForm.value.startTime);
this.timeStart = this.mySummaryForm.value.startTime; //event.target.value;
console.log("select+++startTime : " + this.timeStart);
console.log("change+++segTime : " + this.timeSegment);
}
onKeySeg(event) {
console.log("onKeySeg : " + this.mySummaryForm.value.segmentTime3);
this.timeSegment = this.mySummaryForm.value.segmentTime3;
console.log("select---startTime : " + this.timeStart);
console.log("change---segTime : " + this.timeSegment);
}
checkTimeValue() {
console.log("checkTimeValue :");
if((this.timeSegment < this.timeStart)) {
console.log("a : " + this.timeSegment + " > " + this.startTime);
// alert("good choice!");
}
else {
console.log("b : " + this.startTime + " < " + this.timeSegment);
// alert("time segment should be later than start time.");
}
}
Here is my .html code:
Start time : {{mySummaryForm.value.startTime |date: 'shortTime' | json}}
Segment time : {{mySummaryForm.value.segmentTime3 |date: 'shortTime' | json}}
解决方案
What I endup doing :
my .ts code
onChangeStart(event, label) {
console.log("EVENT : " + event);
if (label == 'start_time') {
this.timeStart = event;
}
}
onChangeSeg(event, label, index) {
if (label == 'segmentTime3') {
this.timeSegment = event;
console.log("this.timeSegment : " + this.timeSegment);
}
if (this.timeSegment > this.timeStart) {
console.log(this.timeSegment + " > " +this.timeStart);
}
else {
console.log(this.timeSegment + " < " +this.timeStart);
alert("the segment time is wrong ! The segment time should be later than the start time. ");
}
}
my .html code
Start time :
Segment time :
There might be a better option but this works for me.