我的 Typescript 类中有三个变量:
A:number;
B:number;
C:number;
在类(class)的另一部分,我尝试添加两个变量 A 和 B:
this.C = this.A+this.B; // A =20 and B = 50;
我在html模板中显示C
<span>{{C}}</span>
我的问题是,我没有得到两个变量 (20+50=70)
的加法,而是得到了连接 (2050)!!
有人可以帮帮我吗?
更新:
这是导致问题的确切代码部分:
goTo(page:number,type:script) {
//
this.pageFirstLineNumber = page;
this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!!
}
注意 pageLastNumber 被声明为数字类型,LINE_OFFSET 是其他数字类型,我找到了解决这个问题的方法,但是 typescript 编译器输出错误(禁止 eval):
////
....
this.pageFirstLineNumber = eval(page.toString()); // now It works !!
this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!!
更新
这里是 LINE_OFFSET 变量的声明:
private _calculateOffset(fontSize: number) {
let linesDiff = (fontSize * 27) / 14;
let lines:number = 27 - (linesDiff - 27);
this.LINE_OFFSET = Math.floor(lines);
if (fontSize >= 17 && fontSize <= 20) {
this.LINE_OFFSET += (Math.floor(fontSize / 3) - 2);
}
if (fontSize > 20 && fontSize <= 23) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) - 2);
}
if (fontSize > 23 && fontSize <= 25) {
this.LINE_OFFSET += (Math.floor(fontSize / 2));}
if (fontSize > 25 && fontSize <= 27) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) + 1);
}
if (fontSize > 27 && fontSize <= 30) {
this.LINE_OFFSET += (Math.floor(fontSize / 2) + 4);
}
}
最佳答案
关于javascript - typescript : Trying the addition of two variables but get the concatenation of the two,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269701/