[(ngModel)] numeric inputs are passed as string values to TypeScript's numeric properties
See original GitHub issueI am testing a simple calculator which should sum 2 numbers but it concatanates them although I declare properties with type definitions.
It’s OK if I code as +this.number1 + +this.number2
. But isn’t there any way to pass them from [(ngModel)]
to typescript class as numeric values?
I have researched but cannot find anything. Am I missing something?
app.component.html
...
<TextField hint="Number 1" [(ngModel)]="number1" keyboardType="number"></TextField>
<TextField hint="Number 2" [(ngModel)]="number2" keyboardType="number"></TextField>
<Button text="Calculate" (tap)="calculate()"></Button>
...
app.component.ts
...
public number1: number = 0;
public number2: number = 0;
public result: number = 0;
calculate(){
this.result = this.number1 + this.number2;
// if I code this as "this.result = +this.number1 + +this.number2;" it is working.
}
...
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Angular4 ngModel changes type of data from 'number' to 'string'
Issue comes into play because of an input field typed as 'text' bound to Typescript 'number' field. Seems to be changing the type...
Read more >How To Convert string to number in Angular/Typescript
To convert string to number in angular or typescript follow the below steps 1.Check if a string is number or not using Number()...
Read more >FormControlName - Angular
Accepts a name as a string or a number. The name in the form of a string is useful for individual forms, while...
Read more >ngModel - AngularJS: API
The ngModel directive binds an input , select , textarea (or custom form control) to a property on the scope using NgModelController, which...
Read more >How to Avoid the Infamous "Cannot read properties of ... - Bitovi
In Regular Type Check Mode, TypeScript is aware that a function might return undefined . But at the same time, TypeScript infers the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
boss this is not working
Simply you can achieve this using JS Number function to convert it into Number https://www.w3schools.com/jsref/jsref_number.asp