如何在REACTIVE Angular表单项目中管理选择选项的数组?

我有这个表单项目,该表单项目具有表单控件,应该具有表单数组。 我已经做完所有准备工作,但是我的选项列表列表为空。 而且,我无法弄清楚如何根据上一个选择来同时选择另一个选择。 (在我看来,它看起来像美国(由用户选择)->其他选择原来是美国各州的列表。用户选择了其他选项,例如印度->在另一个选择中的印度各州的列表)。>

请帮助我实施此实现。我自己弄不清楚,似乎也找不到合适的例子。

我的代码是:

模板:

    <h2>Basic Details</h2>
<form class="form" [formGroup]="signUpForm" (ngSubmit)="onSubmit()">
    <div class="form__group left">
        <div class="form__control form__first-name">
            <input type="text" name="Fname" required formControlName="">
            <label>First Name</label>
        </div>
        <div class="form__control form__email">
            <input type="email" name="email" required formControlName="email">
            <label>Email ID</label>
        </div>
        <div class="form__control form__country" >
            <span>Country</span>
            <select name="country">
                <option *ngFor="let country of countries; let i = index" [value]="country" formControlName="country">{{ country }}</option>
            </select>
        </div>
        <div class="form__control form__phone">
            <input type="number" name="phone" required formControlName="phone">
            <label>Phone Number</label>
        </div>
        <div class="form__control form__button--reset">
            <button type="button" (click)="onReset()">Reset All</button>
        </div>
    </div>
    <div class="form__group right">
        <div class="form__control form__last-name">
            <input type="text" name="Lname" required formControlName="lastName">
            <label>Last Name</label>
        </div>
        <div class="form__control form__ID">
            <input type="text" name="ID" required formControlName="id">
            <label>Your User ID</label>
        </div>
        <div class="form__control form__location">
            <div class="form__location--state">
                <span>State</span>
                <select name="state" formArrayName="state">
                </select>
            </div>
            <div class="form__location--city">
                <span>City</span>
                <select name="city" formArrayName="city">
                </select>
            </div>
        </div>
        <div class="form__control form__code">
            <input type="text" name="code" required formControlName="code">
            <label>Reference Code</label>
        </div>
        <div class="form__control form__button--submit">
            <button type="submit">Continue</button>
        </div>
    </div>
</form>

TS代码:

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

@Component({
  selector: 'app-form',templateUrl: './form.component.html',styleUrls: ['./form.component.scss']
})
export class FormComponent implements OnInit {
  signUpForm: FormGroup;
  countries = ['USA','India'];

  constructor() { }

  ngOnInit() {
    this.signUpForm = new FormGroup({
      'firstName': new FormControl(null),'email': new FormControl(null),'formCountries': new FormArray([]),'phone': new FormControl(null),'lastName': new FormControl(null),'id': new FormControl(null),'state': new FormArray([]),'city': new FormArray([]),'code': new FormControl(null)
    })
  }

}
a714618777 回答:如何在REACTIVE Angular表单项目中管理选择选项的数组?

您要FormArray正确吗?因为FormArray意味着您有FormControl个数组。如果只希望从一个选择中选择多个状态,例如将FormArray更改为FormControl

let myOptions: {viewValue: string,value: string,disabled: boolean} = [
  {
    viewValue: "Audi",value: "Audi",disabled: true
  }
];
<select formControlName="myControlName" multiple>
  <option *ngFor="let opt of myOptions" [disabled]="opt.disabled" [value]="opt.value">
    {{opt.viewValue}}
  </option>
</select>
myCountries: {
  name: string,states: {
    name: string,cities: {
      name: string,postcode: number
    }[]
  }[]
}[] = [
  name: 'USA',states: [
    {
      name: 'California',cities: [
        {
           name: 'My City',postcode: '123456'
        }
      ]
    }
  ]
]
本文链接:https://www.f2er.com/3168372.html

大家都在问