【发布时间】: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