为什么这个Angular6 onclick 函数不能正常工作?
Why does this Angular 6 onclick function not working properly?
因此,我构建了一个快速的 Angular6 应用程序来帮助根据用户想要计算的时间段的开始和结束日期来估算账单金额。 (例如,1 和 5 将 return 从 1 日到 5 日的账单总额)。
无论如何,我有一个按钮 运行 单击时具有以下功能(this.bills$ 是我使用的 json 数据)但是当我输入高于 startDay 时它不起作用结束日。
estimateBills() {
this.estimateText = 0;
console.log('00: startDay is: ' + this.startDay + ' and, endDay is: ' + this.endDay);
if (this.startDay < this.endDay) {
console.log('1: startDay is: ' + this.startDay + ' and, endDay is: ' + this.endDay);
for (var i = 0; i < this.bills$.length; i++) {
if (this.startDay <= this.bills$[i].DueDate && this.endDay >= this.bills$[i].DueDate) {
console.log('Bill DueDate is: ' + this.bills$[i].DueDate + ' and Amount is ' + this.bills$[i].Amount);
this.estimateText = +this.estimateText + this.bills$[i].Amount;
}
}
}
else if (this.startDay > this.endDay) {
console.log('2: startDay is: ' + this.startDay + ' and, endDay is: ' + this.endDay);
for (var i = 0; i < this.bills$.length; i++) {
console.log('looping');
if (this.startDay <= this.bills$[i].DueDate || this.endDay >= this.bills$[i].DueDate) {
console.log('Bill DueDate is: ' + this.bills$[i].DueDate + ' and Amount is ' + this.bills$[i].Amount);
this.estimateText = +this.estimateText + this.bills$[i].Amount;
}
}
}
else {
this.estimateText = 1;
}
}
以下是我在 运行 函数时得到的一些示例。由于某种原因,它没有意识到 startDay 高于 endDay!我知道出了什么问题
When startDay is smaller than endDay (correct output)
When endDay is smaller than startDay (incorrect output)
最有可能的原因是 this.startDay
和 this.endDay
都是字符串,因此 "28" < "4"
比较是 true
(第一个字符 '2' 和 '4' 是在这种情况下进行比较)。
显式转换为数字可能会有所帮助,即 Number(this.startDay) < Number(this.endDay)
。
因此,我构建了一个快速的 Angular6 应用程序来帮助根据用户想要计算的时间段的开始和结束日期来估算账单金额。 (例如,1 和 5 将 return 从 1 日到 5 日的账单总额)。
无论如何,我有一个按钮 运行 单击时具有以下功能(this.bills$ 是我使用的 json 数据)但是当我输入高于 startDay 时它不起作用结束日。
estimateBills() {
this.estimateText = 0;
console.log('00: startDay is: ' + this.startDay + ' and, endDay is: ' + this.endDay);
if (this.startDay < this.endDay) {
console.log('1: startDay is: ' + this.startDay + ' and, endDay is: ' + this.endDay);
for (var i = 0; i < this.bills$.length; i++) {
if (this.startDay <= this.bills$[i].DueDate && this.endDay >= this.bills$[i].DueDate) {
console.log('Bill DueDate is: ' + this.bills$[i].DueDate + ' and Amount is ' + this.bills$[i].Amount);
this.estimateText = +this.estimateText + this.bills$[i].Amount;
}
}
}
else if (this.startDay > this.endDay) {
console.log('2: startDay is: ' + this.startDay + ' and, endDay is: ' + this.endDay);
for (var i = 0; i < this.bills$.length; i++) {
console.log('looping');
if (this.startDay <= this.bills$[i].DueDate || this.endDay >= this.bills$[i].DueDate) {
console.log('Bill DueDate is: ' + this.bills$[i].DueDate + ' and Amount is ' + this.bills$[i].Amount);
this.estimateText = +this.estimateText + this.bills$[i].Amount;
}
}
}
else {
this.estimateText = 1;
}
}
以下是我在 运行 函数时得到的一些示例。由于某种原因,它没有意识到 startDay 高于 endDay!我知道出了什么问题
When startDay is smaller than endDay (correct output)
When endDay is smaller than startDay (incorrect output)
最有可能的原因是 this.startDay
和 this.endDay
都是字符串,因此 "28" < "4"
比较是 true
(第一个字符 '2' 和 '4' 是在这种情况下进行比较)。
显式转换为数字可能会有所帮助,即 Number(this.startDay) < Number(this.endDay)
。