I have created two input fields, one for selecting a date and the other for entering a time. Before submitting the form to the backend, I need to combine these two inputs into one variable. For example,
<input type="text" name="myDate">
and <input type="text" name="myTime">
should be combined to create a new variable called myDateTime
.
Below is an example code snippet and a demo showcasing this functionality.
HTML
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)" novalidate>
<div class="form-group">
<div class="date-ctrl">
<label> Date</label>
<input class="form-control short" [(ngModel)]="myDate" matInput [matDatepicker]="picker"
(dateInput)="addEvent('input', $event)" [ngModelOptions]="{standalone: true}">
<mat-datepicker-toggle class="img-calendar" matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</div>
<div class="time-ctrl">
<label> Time</label>
<input class="form-control shot" [(ngModel)]="myTime" />
</div>
</div>
<div class="footer-dialog">
<div class="field-bottom">
<span class="ctrl-button">
<button class="dialog-button save-color" mat-button type="submit">Save</button>
</span>
</div>
</div>
</form>
Component
createForm() {
this.form = this.formBuilder.group({
myDate: [''],
myTime: ['']
});
// const myDateTime = myDate + myTime
}
onSubmit() {
console.log(this.form.value)
}
Click here for a live demo for further reference.