How to use formControlName and deal with nested formGroup?

后端 未结 3 775
囚心锁ツ
囚心锁ツ 2020-12-07 20:12

As Angular documentation says we can use formControlName in our forms:

Hero Detail

FormControl in a Form

相关标签:
3条回答
  • 2020-12-07 20:40

    you can use Form group which is basically a collection of controls ( controls mean the fields given in your html form) define in your typescript syntax and binded to your HTML elements using the formControlName directive ,for example

    this.myForm = fb.group({
        'fullname': ['', Validators.required],
        'gender': [],
        'address': fb.group({
            'street': [''],
            'houseNumber': [''],
            'postalCode': ['']
        })
    });
    

    Template:

    <form [formGroup]="myForm" >
       <div class="form-group">
          <label for="fullname">Username</label>
          <input type="text" id="username" formControlName="fullname" class="form-control">            
       </div>
       <div class="radio" *ngFor="let gender of genders">
          <label>
          <input type="radio" formControlName="gender" [value]="gender">{{ gender }} </label>
       </div>
       <div formGroupName="address">
          <div class="form-group">
             <label for="street">Username</label>
             <input type="text" id="username" value="street" formControlName="street" class="form-control">            
          </div>
          <div class="form-group">
             <label for="houseNumber">Username</label>
             <input type="text" id="username" value="street" formControlName="houseNumber" class="form-control">            
          </div>
          <div class="form-group">
             <label for="postalCode">Username</label>
             <input type="text" id="username" value="street" formControlName="postalCode" class="form-control">            
          </div>
       </div>
    </form>
    

    A formGroup can consist of a nested formGroup and hierarchy can continue on ,but in accessing the value the its fairly simple .

    0 讨论(0)
  • 2020-12-07 20:50

    tl;dr:

    You can break your form up into components that use your nested formgroups, and formcontrolname can be used as normal.

    The approach I tend to use, since usually nested Formgroups are used to designate separate parts of a form, is to break it up into components and pass those components the nested formgroup as an input parameter. So in your case, I would have an address component that takes a formgroup as a parameter:

    <app-address [formGroup]="myForm.get('address')"></app-address
    

    And inside of that component, I would just have an @Input() formGroup that would be used in the html:

    <div [formGroup]="formGroup">
    ....
    

    That way you can reference the control name explicitly as you normally would, since it would be part of this formgroup.

    Also, keep in mind the form is passed as reference. your changes would be accounted for in the parent component's myForm element, and if you needed access to parts of the form not in you formgroup (validation, change detection, ect ect) you could always pass down the whole form and just define the formgroup to reference the inner group explicitly:

    <div [formGroup]="formGroup.get('adress')">
    

    (assuming you pass down the entire form object that is

    Happy coding!

    0 讨论(0)
  • 2020-12-07 20:59

    It is true. Look at formGroupName

    this.myForm = fb.group({
        'fullname': ['', Validators.required],
        'gender': [],
        'address': fb.group({
            'street': [''],
            'houseNumber': [''],
            'postalCode': ['']
        })
    });
    
    <form [formGroup]="myForm" >
       <div class="form-group">
          <label for="fullname">Username</label>
          <input type="text" id="username" formControlName="fullname" class="form-control">            
       </div>
       <div class="radio" *ngFor="let gender of genders">
          <label>
          <input type="radio" formControlName="gender" [value]="gender">{{ gender }} </label>
       </div>
       <div formGroupName="address">
          <div class="form-group">
             <label for="street">Username</label>
             <input type="text" id="username" value="street" formControlName="street" class="form-control">            
          </div>
          <div class="form-group">
             <label for="houseNumber">Username</label>
             <input type="text" id="username" value="street" formControlName="houseNumber" class="form-control">            
          </div>
          <div class="form-group">
             <label for="postalCode">Username</label>
             <input type="text" id="username" value="street" formControlName="postalCode" class="form-control">            
          </div>
       </div>
    </form>
    
    0 讨论(0)
提交回复
热议问题