0

Ionic 2 and Angular 2

I have 3 textboxes each one with different ngModel value Home.html file:

 <ion-item>
   <ion-label>Off Peak Usage1:</ion-label>     
   <ion-input type="text" [(ngModel)]="usage.usage1"></ion-input> 
 </ion-item>
 <ion-item>
   <ion-label>Off Peak Usage2:</ion-label>     
   <ion-input type="text" [(ngModel)]="usage.usage2"></ion-input>   
 </ion-item>
 <ion-item>
   <ion-label>Off Peak Usage3:</ion-label>     
   <ion-input type="text" [(ngModel)]="usage.usage3"></ion-input> 
 </ion-item>

I want to do some calculation part on those entered values in textboxes one after other. I want each text box value must be passed one after other. Below is my For loop in Home.ts file:

usage={
    usage1:'',
    usage2:'',
    usage3:''
};
for(let i=1; i<=3; i++)
{
    let total_usage = this.usage.usage[i];
    let total = total_usage * 5;
}

I am getting usage not defined on this.usage.usage[i]

I don't know I am doing correctly or not, can any one help me out with this ?

Thank you.

2
  • this.usage.usage[i] you have three different properties usage1,usage2,usage3 not an array of usage. Commented Apr 27, 2017 at 10:11
  • @suraj thank you. so how can use those property values in an array ? Commented Apr 27, 2017 at 10:14

1 Answer 1

1

Try to access object property like so:

this.usage['usage1']

Your loop will be:

for(let i=1; i<=3; i++)
{
    let total_usage = this.usage['usage'+i];
    let total = total_usage * 5;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It worked for me. Thank you so much for your quick reply.

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.