I have three variables in the Typescript class:
A:number; B:number; C:number;
in another part of the class, I am trying to add two variables A and B:
this.C = this.A+this.B; // A =20 and B = 50;
and I show C in the html template
<span>{{C}}</span>
My problem is that instead of adding the TWO variable (20+50=70) I get concatenation (2050) !!
Can anybody help me?
UPDATE :
Here is the exact piece of code that causes the problem:
goTo(page:number,type:script) { // this.pageFirstLineNumber = page; this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!! }
Please note that pageLastNumber is declared as the type of number, LINE_OFFSET is the type of number, I found a solution to this problem, but the Typescript compiler generates an error (forbidden evaluation):
//// .... this.pageFirstLineNumber = eval(page.toString()); // now It works !! this.pageLastLineNumber = page + this.LINE_OFFSET; //concatenation!!
UPDATE
Here is the declaration of the LINE_OFFSET variable:
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); } }
source share