首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular嵌套formGroups

Angular嵌套formGroups
EN

Stack Overflow用户
提问于 2020-01-31 19:00:36
回答 3查看 466关注 0票数 0

我在使用数据更新表单时遇到了一些问题。我在另一个formGroup中嵌套了一个formGroup,但是我得到的数据并没有更新表单,它仍然是空的。我可以在日志中看到数据,所以表单更新有问题。

下面是我的组件:

代码语言:javascript
复制
this.editForm = this.formBuilder.group({
      id: [],
      date: ['', Validators.required],
      checkboxValue: this.formBuilder.group({ 
        closed: [],
        openFrom: [''],
        openTo: [''],
      })
    });

    console.log(this.editForm.value);
    this.httpClientService.getIrregularDaysById(+irregDayId)
      .subscribe(data => {
        this.irregDay = data;
        this.irregDay.openFrom = this.formatTime(this.irregDay.openFrom);
        this.irregDay.openTo = this.formatTime(this.irregDay.openTo);
        // this.irregDay.date = this.formatDate(this.irregDay.date);
        this.irregDay.closed = this.formatClosed(this.irregDay.closed);

        this.editForm.patchValue(data);
        console.log('getIrregularDaysById: ', data);
        console.log(data.date);
      });

和我的HTML:

代码语言:javascript
复制
<div class="col-md-6">
  <h2 class="text-center">Edit irregular day</h2>
  <form [formGroup]="editForm" (ngSubmit)="onSubmit()">

    <div class="material-input">
      <mat-form-field class="form-group" appearance="outline">
        <input matInput readonly [matDatepicker]="picker" placeholder="Please pick a date" formControlName="date">
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker touchUi #picker disabled="false"></mat-datepicker>
        <mat-error *ngIf="editForm.get('date').errors && (editForm.get('date').touched)">
          Date is required
        </mat-error>
      </mat-form-field>
    </div>

    <div formGroupName="checkboxValue">
      <div class="form-group">
        <mat-checkbox formControlName="closed" class="form-control" color="primary" [(ngModel)]="disabled">
          Closed</mat-checkbox>
      </div>

      <div class="material-input">
        <mat-form-field class="form-group" appearance="outline">
          <mat-label>Open from</mat-label>
          <div class="timepicker">
            <input matInput readonly [disableClick]="true" placeholder="Open from" formControlName="openFrom"
              [ngxTimepicker]="startTimePicker" [format]="24" [disabled]="disabled">
            <ngx-material-timepicker #startTimePicker></ngx-material-timepicker>
            <ngx-material-timepicker-toggle [for]="startTimePicker"></ngx-material-timepicker-toggle>
          </div>
          <mat-error *ngIf="editForm.get('openFrom')?.errors && (editForm.get('openFrom').touched)">
            This field is required
          </mat-error>
        </mat-form-field>
      </div>

      <div class="material-input">
        <mat-form-field class="form-group" appearance="outline">
          <mat-label>Open to</mat-label>
          <div class="timepicker">
            <input matInput readonly [disableClick]="true" placeholder="Open to" formControlName="openTo"
              [ngxTimepicker]="startTimePicker2" [format]="24" [disabled]="disabled">
            <ngx-material-timepicker #startTimePicker2></ngx-material-timepicker>
            <ngx-material-timepicker-toggle [for]="startTimePicker2"></ngx-material-timepicker-toggle>
          </div>
          <mat-error *ngIf="editForm.get('openTo')?.errors && (editForm.get('openTo').touched)">
            This field is required
          </mat-error>
        </mat-form-field>
      </div>
    </div>

    <button mat-raised-button color="primary" type="button" class="btn btn-success" [disabled]="!editForm.valid"
      (click)="onSubmit()" routerLink="/irregulardays">Update</button>
  </form>
</div>
EN

回答 3

Stack Overflow用户

发布于 2020-01-31 19:33:51

你的设置是这样的

代码语言:javascript
复制
this.editForm.patchValue({
    id: ........,
    date: ......
})
票数 1
EN

Stack Overflow用户

发布于 2020-02-01 21:54:02

必须为formGroup patchvalue方法提供具有键和值对的参数。

代码语言:javascript
复制
formgroup.patchValue({name:’Mocrosoft’});

在你的情况下。

代码语言:javascript
复制
this.editForm.patchValue(
                {   
                  id: data.id,
                  date: data.date, {
                  checkboxValue: { 
                  openFrom: true 
                }}});

我在谷歌上找到了一个最好的例子,那就是这个。https://stackblitz.com/edit/angular-patch-value-deeply-nested-component

票数 0
EN

Stack Overflow用户

发布于 2020-02-01 22:18:56

如果你想更新FormGroup,你可以使用patchValue方法。此方法接受与FormGroup控件名称相同的键作为参数对象。

例如:

代码语言:javascript
复制
this.editForm.patchValue({
      id: data.id,
      date: data.date
      checkboxValue: {...data.checkboxValue}
    }

因此,如果checkboxValue也是FormGroup,则可以将此控件更新为:

代码语言:javascript
复制
this.editForm.get('checkboxValue').patchValue({...data.checkboxValue});

你的解决方案。我不知道订阅中数据获取的接口,但我认为它可以是正确的:

代码语言:javascript
复制
this.httpClientService.getIrregularDaysById(+irregDayId).subscribe(data => {
  this.irregDay = {...data};
  this.irregDay.openFrom = this.formatTime(data.openFrom);
  this.irregDay.openTo = this.formatTime(data.openTo);
  this.irregDay.date = this.formatDate(data.date);
  this.irregDay.closed = this.formatClosed(data.closed);

  this.editForm.patchValue({
    id: data.id,
    date: data.date or this.irregDay.date if you need formatted data
    checkboxValue: {
      closed: data.closed or this.irregDay.closed if you need formatted data
      openFrom:  data.openFrom or this.irregDay.openFrom if you need formatted data
      openTo:  data.openTo or this.irregDay.openTo if you need formatted data
    }
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60002605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档