question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[(ngModel)] numeric inputs are passed as string values to TypeScript's numeric properties

See original GitHub issue

I 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.
}
...

stackoverflow

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
radhakrishnan0404commented, Jun 20, 2017

boss this is not working

0reactions
Sachin124commented, May 13, 2019

Simply you can achieve this using JS Number function to convert it into Number https://www.w3schools.com/jsref/jsref_number.asp

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found