angularjs 如何将数组数据放在不同的mat-form-field中?

nzk0hqpo  于 6个月前  发布在  Angular
关注(0)|答案(1)|浏览(48)

我有一个数组,保存4个数据:[onHour,onMinute,offHour,offMinute],我有4个数据,它们不在数组中。

<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>

字符串
在.ts组件中,我有这个变量periodicValues = [this.hourGlowPeriodic?.value, this.minuteGlowPeriodic?.value, this.hourSilencePeriodic?.value, this.minuteSilencePeriodic?.value];,它通过提交按钮填充界面,但我不知道如何在ngOnInIt上填充periodicValues[]数组
我试过这种形式,但没有工作

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 }),
});

o75abkj4

o75abkj41#

您可以使用值hour_silence_periodicminute_silence_periodic更新HTML中的最后两个formControlName,除此之外,根据提供的数据,您的代码似乎很好,请找到下面的工作示例,如果复制,请与问题共享。

import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  ReactiveFormsModule,
} from '@angular/forms';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { provideAnimations } from '@angular/platform-browser/animations';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [
    ReactiveFormsModule,
    CommonModule,
    MatFormFieldModule,
    MatInputModule,
    MatIconModule,
  ],
  template: `
 <form [formGroup]="form">
    <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="hour_silence_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="minute_silence_periodic" min="0" max="24">
       </mat-form-field>
    </div>
 </form>
  `,
})
export class App {
  name = 'Angular';
  data = {
    element: {
      periodic_values: [1, 2, 3, 4],
    },
  };
  form: FormGroup = new FormGroup({});

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    this.form = this.formBuilder.group({
      hour_glow_periodic: new FormControl({
        value: this.data.element?.periodic_values[0],
        disabled: false,
      }),
      minute_glow_periodic: new FormControl({
        value: this.data.element?.periodic_values[1],
        disabled: false,
      }),
      hour_silence_periodic: new FormControl({
        value: this.data.element?.periodic_values[2],
        disabled: false,
      }),
      minute_silence_periodic: new FormControl({
        value: this.data.element?.periodic_values[3],
        disabled: false,
      }),
    });
  }
}

bootstrapApplication(App, {
  providers: [provideAnimations()],
});

字符串
stackblitz

相关问题