FormGroupName

Syncs a nested FormGroup to a DOM element.

查看"说明"...

参见

NgModule

选择器

属性

属性说明
@Input('formGroupName')
name: string

Tracks the name of the FormGroup bound to the directive. The name corresponds to a key in the parent FormGroup or FormArray.

继承自 AbstractFormGroupDirective

继承自 ControlContainer

继承自 AbstractControlDirective

说明

This directive can only be used with a parent FormGroupDirective.

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

Use nested form groups to validate a sub-group of a form separately from the rest or to group the values of certain controls into their own nested object.

Access the group by name

The following example uses the get method to access the associated FormGroup

this.form.get('name');
      
      this.form.get('name');
    

Access individual controls in the group

The following example uses the get method to access individual controls within the group using dot syntax.

this.form.get('name.first');
      
      this.form.get('name.first');
    

Register a nested FormGroup.

The following example registers a nested name FormGroup within an existing FormGroup, and provides methods to retrieve the nested FormGroup and individual controls.

import {Component} from '@angular/core'; import {FormControl, FormGroup, Validators} from '@angular/forms'; @Component({ selector: 'example-app', template: ` <form [formGroup]="form" (ngSubmit)="onSubmit()"> <p *ngIf="name.invalid">Name is invalid.</p> <div formGroupName="name"> <input formControlName="first" placeholder="First name"> <input formControlName="last" placeholder="Last name"> </div> <input formControlName="email" placeholder="Email"> <button type="submit">Submit</button> </form> <button (click)="setPreset()">Set preset</button> `, }) export class NestedFormGroupComp { form = new FormGroup({ name: new FormGroup({ first: new FormControl('Nancy', Validators.minLength(2)), last: new FormControl('Drew', Validators.required) }), email: new FormControl() }); get first(): any { return this.form.get('name.first'); } get name(): any { return this.form.get('name'); } onSubmit() { console.log(this.first.value); // 'Nancy' console.log(this.name.value); // {first: 'Nancy', last: 'Drew'} console.log(this.form.value); // {name: {first: 'Nancy', last: 'Drew'}, email: ''} console.log(this.form.status); // VALID } setPreset() { this.name.setValue({first: 'Bess', last: 'Marvin'}); } }
      
      
  1. import {Component} from '@angular/core';
  2. import {FormControl, FormGroup, Validators} from '@angular/forms';
  3.  
  4. @Component({
  5. selector: 'example-app',
  6. template: `
  7. <form [formGroup]="form" (ngSubmit)="onSubmit()">
  8. <p *ngIf="name.invalid">Name is invalid.</p>
  9.  
  10. <div formGroupName="name">
  11. <input formControlName="first" placeholder="First name">
  12. <input formControlName="last" placeholder="Last name">
  13. </div>
  14. <input formControlName="email" placeholder="Email">
  15. <button type="submit">Submit</button>
  16. </form>
  17.  
  18. <button (click)="setPreset()">Set preset</button>
  19. `,
  20. })
  21. export class NestedFormGroupComp {
  22. form = new FormGroup({
  23. name: new FormGroup({
  24. first: new FormControl('Nancy', Validators.minLength(2)),
  25. last: new FormControl('Drew', Validators.required)
  26. }),
  27. email: new FormControl()
  28. });
  29.  
  30. get first(): any { return this.form.get('name.first'); }
  31.  
  32. get name(): any { return this.form.get('name'); }
  33.  
  34. onSubmit() {
  35. console.log(this.first.value); // 'Nancy'
  36. console.log(this.name.value); // {first: 'Nancy', last: 'Drew'}
  37. console.log(this.form.value); // {name: {first: 'Nancy', last: 'Drew'}, email: ''}
  38. console.log(this.form.status); // VALID
  39. }
  40.  
  41. setPreset() { this.name.setValue({first: 'Bess', last: 'Marvin'}); }
  42. }

继承自 AbstractFormGroupDirective

继承自 AbstractControlDirective