【问题标题】:How do I manage arrays of select options in an REACTIVE Angular-form project?如何管理 REACTIVE Angular-form 项目中的选择选项数组?
【发布时间】:2019-11-04 18:18:12
【问题描述】:

我有这个表单项目,它有表单控件并且应该有表单数组。 我已经完成了所有的准备工作,但是我的选项列表是空的。 而且,我不知道如何根据前一个选择来预先流行另一个选择。 (在我看来,它看起来像美国(由用户选择)-> 其他选择结果是美国各州的列表。用户选择了其他选项,例如印度-> 另一个选择中的印度州列表)。

请帮我完成这个实现。我自己无法弄清楚,似乎找不到合适的例子。

我的代码是:

模板:

    <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)
    })
  }

}

【问题讨论】:

    标签: javascript html angular angular-reactive-forms


    【解决方案1】:

    您想要FormArray 对吗?因为FormArray 意味着你有一个FormControls 的数组。如果您只想从一个选择中选择多个例如状态,请将您的 FormArrays 更改为 FormControls。

    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'
            }
          ]
        }
      ]
    ]
    

    【讨论】:

    • 像这样:
      城市
    • 我不明白如何显示选择选项?例如这里我做错了什么:
      Country
    • 选项不能是FormControl。您的selectFormControl。要禁用option,请设置属性disabled
    • 看看我的编辑。代替我的选项的disabled 属性,您可以提供一个功能来检查您的选项是否被禁用。
    • 好的,我有点明白你的意思,但我想要一个控件数组,这些控件将由 ngFor 循环并在模板中呈现,我想做一个会影响另一个的选择选择...这是我的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2021-11-13
    • 2023-04-02
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多