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

Simon West
3,7881 gold badge28 silver badges28 bronze badges
asked Mar 8, 2017 at 8:25
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)

answered Mar 8, 2017 at 12:10
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);
answered Jun 4, 2020 at 8:31

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

answered Mar 8, 2017 at 8:57

Comments

0

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

answered Mar 8, 2017 at 12:02

Comments

0

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

answered Sep 25, 2017 at 21:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.