[英]Why does this Angular 6 onclick function not working properly?
因此,我構建了一個快速的 Angular 6 應用程序,以根據用戶想要計算的時間段的開始和結束日期來幫助估算賬單金額。 (例如,1 和 5 將返回從 1 日到 5 日的賬單總額)。
無論如何,我有一個按鈕在單擊時運行以下功能(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; } }
以下是我在運行該函數時得到的一些示例。 出於某種原因,它沒有意識到 startDay 高於 endDay! 知道出了什么問題
最可能的原因是this.startDay
和this.endDay
都是字符串,因此"28" < "4"
比較為true
(在這種情況下比較第一個字符 '2' 和 '4')。
顯式轉換為數字可能會有所幫助,即Number(this.startDay) < Number(this.endDay)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.