0

I have 2 input values Here is how it look like

<div class="form-group">
                    <label>{{l("RoomLength")}}</label>
                    <input number-directive #roomLengthInput="ngModel" class="form-control nospinner-input" type="text" name="roomLength" [(ngModel)]="room.roomLength"   maxlength="32">
                </div>
                <div class="form-group">
                    <label>{{l("RoomWidth")}}</label>
                    <input number-directive #roomWidthInput="ngModel" class="form-control nospinner-input" type="text" name="roomWidth" [(ngModel)]="room.roomWidth"   maxlength="32">
                </div>

And I need to concat those values into one value and bind to this input

<div class="form-group">
                    <label>{{l("TotalSize")}}</label>
                    <input number-directive #totalSizeInput="ngModel" class="form-control nospinner-input" type="text" name="totalSize" [(ngModel)]="room.totalSize"   maxlength="32">
                </div>

How I can do this?

8
  • 1
    How is it supposed to work when you edit totalSize? Should it modify roomLength or roomWidth? Commented Apr 11, 2019 at 18:02
  • Nope. It should not update, this field will be readonly @ConnorsFan Commented Apr 11, 2019 at 18:05
  • And what is the mathematical relationship between the three values? Commented Apr 11, 2019 at 18:08
  • total = length*width @ConnorsFan Commented Apr 11, 2019 at 18:12
  • You could use one-way binding: [ngModel]="room.roomLength * room.roomWidth". Commented Apr 11, 2019 at 18:17

1 Answer 1

1

You can listen for ngModelChange event on both inputs(room.roomLength and room.roomWidth) and update the room.totalSize on eventHandler of that.

<div class="form-group">
                    <label>{{l("RoomLength")}}</label>
                    <input number-directive #roomLengthInput="ngModel" class="form-control nospinner-input" type="text" name="roomLength" [(ngModel)]="room.roomLength" (ngModelChange)="updateSize()"   maxlength="32">
                </div>
                <div class="form-group">
                    <label>{{l("RoomWidth")}}</label>
                    <input number-directive #roomWidthInput="ngModel" class="form-control nospinner-input" type="text" name="roomWidth" [(ngModel)]="room.roomWidth" (ngModelChange)="updateSize()"   maxlength="32">
                </div>

In your component.ts file,

updateSize() {
   this.totalSize = this.room.roomWidth + this.room.roomLength
}
Sign up to request clarification or add additional context in comments.

2 Comments

It helps. Thank's
Glad to help you.

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.