2
totalbalancetemp = (Number(this.balance)) + (Number(this.pastAmount));

My totalbalancetemp is returning undefined whereas this.balance is equal to 34 and this.pastAmount is equal to 23.

I have this in controller and displaying totalbalancetemp using exp in html

1
  • if totalbalancetemp is a property of the component. then shouldn't you call it by this.totalbalancetemp Commented Mar 8, 2017 at 8:28

5 Answers 5

2

Supply the proper type.

let totalbalancetemp:number = balance + pastAmount

This will throw an error, because you are now guaranteeing that totalbalancetemp will be a number.

The type String is not assignable to type 'number'

Try the following:

let balance:string = '34',
    pastAmount:string = '23',
    totalbalancetemp:number = 0

totalbalancetemp = Number(balance) + Number(pastAmount)

alert(totalbalancetemp)

Sign up to request clarification or add additional context in comments.

1 Comment

It is still giving me 0 as totalbalancetemp and not adding these values actually balance is like from json like this.payment.total and this.payment.pastAmount
2
totalbalancetemp = (Number(this.balance)) + (Number(this.pastAmount));

please try this it should work

totalbalancetemp:number = (+this.balance) + (+this.pastAmount);

Comments

0
var totalbalancetemp = null; this.balance = 34; this.pastAmount = 23; 

totalbalancetemp = (Number(this.balance)) + (Number(this.pastAmount));

alert(totalbalancetemp);

-->totalbalancetemp - Define Variable (or) any type

Comments

0

totalbalancetemp should be replaced by this.totalbalancetemp if it's part of the angular 2 component

Comments

0

Do a +this.balance in the .ts file or this.balance*1 or this.balance/1 on in the template file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.