I am dealing with an array that stores 4 data points: [onHour, onMinute, offHour, offMinute]. I also have 4 elements that are not in an array and need to be repeated.
<div class="on">
<mat-form-field appearance="fill" class="time_field">
<mat-label>minute</mat-label>
<input matInput type="number" formControlName="minute_glow_periodic" min="0" max="59">
</mat-form-field>
<mat-form-field appearance="fill" class="time_field">
<mat-label>hour</mat-label>
<input matInput type="number" formControlName="hour_glow_periodic" min="0" max="24">
</mat-form-field>
</div>
<div class="off">
<mat-form-field appearance="fill" class="time_field">
<mat-label>minute</mat-label>
<input matInput type="number" formControlName="minute_glow_periodic" min="0" max="59">
</mat-form-field>
<mat-form-field appearance="fill" class="time_field">
<mat-label>hour</mat-label>
<input matInput type="number" formControlName="hour_glow_periodic" min="0" max="24">
</mat-form-field>
</div>
Within the .ts component, I have a variable
periodicValues = [this.hourGlowPeriodic?.value, this.minuteGlowPeriodic?.value, this.hourSilencePeriodic?.value, this.minuteSilencePeriodic?.value];
that populates the interface upon submit button click. However, I am unsure of how to fill the form control with the periodicValues[]
array in the ngOnInit
lifecycle hook.
I attempted to use this code snippet, but it did not work as expected:
this.form = this.formBuilder.group({
hour_glow_periodic: new UntypedFormControl({ value: this.data.element?.periodic_values[0], disabled: false }),
minute_glow_periodic: new UntypedFormControl({ value: this.data.element?.periodic_values[1], disabled: false }),
hour_silence_periodic: new UntypedFormControl({ value: this.data.element?.periodic_values[2], disabled: false }),
minute_silence_periodic: new UntypedFormControl({ value: this.data.element?.periodic_values[3], disabled: false }),
});