填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

FormArrayName

将嵌套的 FormArray 同步到 DOM 元素。

Syncs a nested FormArray to a DOM element.

查看"说明"...

参见

Exported from

选择器

属性

属性说明
@Input('formArrayName')
name: string | number | null

跟踪绑定到指令 FormArray 的名称。该名称对应于父 FormGroupFormArray 中的键名。接受字符串或数字形式的名称。字符串形式的名称对于单个表单很有用,而数字形式则允许在 FormArray 数组上进行迭代时将表单数组绑定到索引。

Tracks the name of the FormArray bound to the directive. The name corresponds to a key in the parent FormGroup or FormArray. Accepts a name as a string or a number. The name in the form of a string is useful for individual forms, while the numerical form allows for form arrays to be bound to indices when iterating over arrays in a FormArray.

control: FormArray只读

要绑定到此指令的 FormArray

The FormArray bound to this directive.

formDirective: FormGroupDirective | null只读

该组的顶级指令(如果存在),否则为 null。

The top-level directive for this group if present, otherwise null.

path: string[]只读

返回一个数组,该数组表示从顶级表单到此控件的路径。每个索引是该级别上控件的字符串名称。

Returns an array that represents the path from the top-level form to this control. Each index is the string name of the control on that level.

继承自 ControlContainer

继承自 AbstractControlDirective

说明

此指令旨在与父 FormGroupDirective(选择器为 [formGroup] )一起使用。

This directive is designed to be used with a parent FormGroupDirective (selector: [formGroup]).

它接受你要链接的嵌套 FormArray 的字符串名称,并寻找使用这个名字在你传给 FormGroupDirective 的父 FormGroup 实例中注册的 FormGroup

It accepts the string name of the nested FormArray you want to link, and will look for a FormArray registered with that name in the parent FormGroup instance you passed into FormGroupDirective.

例子

Example

      
      import {Component} from '@angular/core';
import {FormArray, FormControl, FormGroup} from '@angular/forms';

@Component({
  selector: 'example-app',
  template: `
    <form [formGroup]="form" (ngSubmit)="onSubmit()">
      <div formArrayName="cities">
        <div *ngFor="let city of cities.controls; index as i">
          <input [formControlName]="i" placeholder="City">
        </div>
      </div>
      <button>Submit</button>
    </form>

    <button (click)="addCity()">Add City</button>
    <button (click)="setPreset()">Set preset</button>
  `,
})
export class NestedFormArray {
  form = new FormGroup({
    cities: new FormArray([
      new FormControl('SF'),
      new FormControl('NY'),
    ]),
  });

  get cities(): FormArray {
    return this.form.get('cities') as FormArray;
  }

  addCity() {
    this.cities.push(new FormControl());
  }

  onSubmit() {
    console.log(this.cities.value);  // ['SF', 'NY']
    console.log(this.form.value);    // { cities: ['SF', 'NY'] }
  }

  setPreset() {
    this.cities.patchValue(['LA', 'MTV']);
  }
}
    

继承自 AbstractControlDirective