In this scenario, I have chosen to utilize a different approach instead of using type="number" because users can easily remove it by pressing F12.
While including
type="number"
in my HTML code does work, it still remains unclear why the minus sign works without explicitly adding it.
The reason behind why the minus sign functions properly while the plus sign doesn't may be due to the fact that - is not considered a string operator, whereas + is.
During my experimentation with TypeScript, I made this observation. When I apply a minus sign, the operation behaves as expected. However, when using the plus sign, the result is unexpected, and I am unable to locate the source of this issue.
Below is my class structure:
export class Add {
number1: Number;
number2: Number;
total: Number;
}
I have attempted to assign them using let within my function:
Math(): void {
let numberone = this.add.number1;
let numbertwo = this.add.number2;
this.add.total = numberone + numbertwo;
}
The anticipated outcome would be:
numberone = 10
numbertwo = 10
this.add.total = numerone + numbertwo
However, instead of the total being 20, it displays 1010.
Below is the corresponding HTML code:
<h2>here you can add stuff</h2>
<div><span>first number: </span><input [(ngModel)]="add.number1"
placeholder="number1"></div>
<div><span>second number: </span><input [(ngModel)]="add.number2"
placeholder="number2"></div>
<button (click)="Math()">press here to add</button> <label>{{add.total}}</label>