简体   繁体   English

为什么这个 Angular 6 onclick 功能不能正常工作?

[英]Why does this Angular 6 onclick function not working properly?

So, I built a quick Angular 6 app to help estimate bill amounts given the start and end date of the period a user wants to calculate for.因此,我构建了一个快速的 Angular 6 应用程序,以根据用户想要计算的时间段的开始和结束日期来帮助估算账单金额。 (ex. 1 and 5 would return the total amount of bills from the 1st to the 5th). (例如,1 和 5 将返回从 1 日到 5 日的账单总额)。

Anyways, I have a button run the following function when clicked (this.bills$ is the json data im using) but its not working when i enter a startDay higher than the endDay.无论如何,我有一个按钮在单击时运行以下功能(this.bills$ 是我使用的 json 数据)但是当我输入的 startDay 高于 endDay 时它不起作用。

 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; } }

Here are some examples of what i get when run the function.以下是我在运行该函数时得到的一些示例。 For some reason it does not realize that the startDay is higher than the endDay!出于某种原因,它没有意识到 startDay 高于 endDay! idk what is going wrong知道出了什么问题

When startDay is smaller than endDay (correct output)当 startDay 小于 endDay 时(正确输出)

When endDay is smaller than startDay (incorrect output)当 endDay 小于 startDay 时(错误输出)

Most probably the reason is that both this.startDay and this.endDay are strings, therefore "28" < "4" comparison is true (first characters '2' and '4' are compared in this case).最可能的原因是this.startDaythis.endDay都是字符串,因此"28" < "4"比较为true (在这种情况下比较第一个字符 '2' 和 '4')。

Explicit cast to numbers may help here, ie Number(this.startDay) < Number(this.endDay) .显式转换为数字可能会有所帮助,即Number(this.startDay) < Number(this.endDay)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM