How to add two numbers in angularjs 2

in input tag i am defining [(ngModel)]= num1 and in other num2
{{num1 + num2}} is showing concatination of num1 and num 2(like in case of string)
please help

these are working properly
{{num1*num2}}
{{num1-num2}}
{{num1/num2}}

Whatever you are trying to do: Just put that into your controller… It does not look like it should be put into a template.

thanks for the suggestion bro but controllers and scope are not there in angularjs 2

I am facing same problem
did you got solution or workaround?

@wtfuii nailed this. I don’t know what more there is to say.

1 Like

To add some additional clarification here, the reason @wtfuii’s suggestion is so great is that you don’t have control over types in templates, and JavaScript’s type conversion is rather baroque. Properties bound to <ion-input> receive strings, and if you don’t bother to explicitly convert them to numbers (such as via Number.parseInt()), the + operator will treat them as strings and concatenate them.

The bottom line is that you should try to do as much in controller code as possible, and minimize work done by template expressions.

Use the following from HTML:

{{num1 * 1 + num2 * 1}}

2 Likes

thank you try to declare the variables as a number but even so it worked
it only worked with your answer

var adults: number = this.adults;
var children : number = this.children;
var babies : number = this.babies;

totalpassengers: number = adults * 1 + children * 1 + babies * 1;