【问题标题】:Get nested reactive form value in Angular?在 Angular 中获取嵌套的响应式表单值?
【发布时间】:2019-10-04 14:25:31
【问题描述】:

我有一个类似于以下的嵌套表单:

profileForm = new FormGroup({
  firstName: new FormControl(''),
  lastName: new FormControl(''),
  address: new FormGroup({
    street: new FormControl(''),
    city: new FormControl(''),
    date1: new FormControl(''),
    date2: new FormControl('')
  })
});

我正在尝试将 date2 mindate 设置为 date1 值,如下所示:

<mat-form-field class="datepickerformfield" floatLabel="never">
    <input matInput class="dp" formControlName="date2" [min]="profileform.controls['date1'].value" [matDatepicker]="date2" placeholder="DD/MM/AAAA" >
</mat-form-field>

也尝试过:

[min]="profileform.address.controls['date1'].value"

[min]="profileform.controls[address].controls['date1'].value"

但我得到一个错误:

无法读取未定义的属性“值”

如何使用profileform 对象获取date1 值?

【问题讨论】:

  • 可以分享到stackbiltz吗
  • profileForm.get('address').get('date1')
  • [min]="profileform.get('address').get('date1').value",或[min]="profileform.get('address.date1').value"

标签: angular forms typescript


【解决方案1】:

终于解决了:

[min]="profileform.get('address.date1').value"

在 Angular6+ 上工作

【讨论】:

    【解决方案2】:

    表单有一个名为value 的成员,它反映了您的表单结构,但仅包含值。 要访问日期,您只需致电[min]="profileform.value.address.date1"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2018-07-14
      • 2019-11-02
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2018-08-06
      相关资源
      最近更新 更多