我在angularjs中收到此错误代码,尝试创建倒数计时器,请帮助
//Convert the remainders gotten above to the nearest whole number
intervalinsecond = (intervalinsecond < 10) ? "0" + intervalinsecond : intervalinsecond;
intervalinminute = (intervalinminute < 10) ? "0" + intervalinminute : intervalinminute;
intervalinhour = (intervalinhour < 10) ? "0" + intervalinhour : intervalinhour;
//Get the value for day and output it throug the ID in the html table tags
document.getElementById("days").textContent = intervalinday;
document.getElementById("days").innerText = intervalinday;
//Get the value for hour and output it throug the ID in the html table tags
document.getElementById("hours").textContent = intervalinhour;
document.getElementById("hours").innerText = intervalinhour;
//Get the value for minute and output it throug the ID in the html table tags
document.getElementById("minutes").textContent = intervalinminute;
document.getElementById("minutes").innerText = intervalinminute;
//Get the value for second and output it throug the ID in the html table tags
document.getElementById("seconds").textContent = intervalinsecond;
document.getElementById("seconds").innerText = intervalinsecond;
答案 0 :(得分:0)
假设intervalinsecond
变量是一个数字,表达式(intervalinsecond < 10) ? "0" + intervalinsecond : intervalinsecond
的结果可以是数字或字符串,具体取决于intervalinsecond
值。因此,TS编译器将整个表达式的类型推断为number | string
。但由于它包含number
以外的类型,因此无法将此类表达式的结果分配给类型为number
的变量。这就是编译器抱怨的内容。
PS。除此之外,使用相同的变量来保存一些数值并将其格式化的表示保存在模板中并不是一个好主意。您希望使用管道直接在模板中格式化原始数值,而不需要任何组件的代码。