0

How can I add the values ​​entered in an input

This is my HTML, But I do not know if I should write something in the ts

<div class="small-7 columns">
  <md-input-container>
    <input type="number" id="fnum" placeholder="fnum">
  </md-input-container>
  <md-input-container>
    <input type="number" id="snum" placeholder="snum">
  </md-input-container>
  <md-input-container>
    <input disabled>{{ (snum) + (fnum) }}
  </md-input-container>
</div>

I would appreciate it if you could help me

1

1 Answer 1

1

You can use ngModel to get the input values and ngModelChange for change detection. Then, add them in your ts and update a third variable that holds the addition.

html:

<md-input-container>
    <input mdInput type="number" id="fnum" placeholder="fnum" 
           [(ngModel)]="fnum" (ngModelChange)="addNums()">
</md-input-container>

<p></p>

<md-input-container>
    <input mdInput type="number" id="snum" placeholder="snum" 
           [(ngModel)]="snum" (ngModelChange)="addNums()">
</md-input-container>

<p></p>  

<md-input-container>
    <input mdInput disabled>{{ total }}
</md-input-container>

In ts:

fnum: number = 0;
snum: number = 0;

total: number = 0;

addNums(){
  this.total = this.fnum + this.snum;
}

demo

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

2 Comments

This is the correct one :). Also, It's worth to mention that the OP is missing one of the most important things: mdInput on <input>. Here's a version without ngModel.
Thank you very much, I have been helped

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.