I have a conditional field in a form like this:
<mat-form-field *ngIf="model.personIsMarried === 'Yes'">
<input type="number"
matInput id="personSpouseAge"
[(ngModel)]="model.personSpouseAge"
name="personSpouseAge"
placeholder="Spouse Age"
</mat-form-field>
If the user is not married the value of model.personSpouseAge
is undefined
. What I want is to set the value of model.personSpouseAge
to the model.personAge
field that was previously filled, for example:
The person filling the form is age 27 and is not married, so the model.personSpouseAge
field value will also be 27
.
I hope to be clear. Thanks for your help.
In html:
[ngModel]="model.personSpouseAge"
(ngModelChange)="setPersonSpouseAge(model)"
In .ts:
setPersonSpouseAge(model) {
model.personSpouseAge = model.personSpouseAge || model.personAge
}