gpt4 book ai didi

Angular react 形式错误 : Must supply a value for form control with name:

转载 作者:太空狗 更新时间:2023-10-29 17:34:30 26 4
gpt4 key购买 nike

我前提是我对 Angular 还是个新手。我正在尝试实现一个 Angular react 形式,但我遇到了这个错误:“必须为名称为表单控件的值提供一个值:目的地。

这是我的组件和 html 的相关部分:

import { Component, Inject } from '@angular/core';
import { Http } from "@angular/http";
import { FormGroup, FormControl, FormBuilder, Validators } from "@angular/forms";

@Component({
selector: 'home',
templateUrl: './home.component.html'
})
export class HomeComponent {

locations: Location[];
flightChoice: FlightChoice;
form: FormGroup;


constructor(http: Http, @Inject('BASE_URL') private baseUrl: string,
private fb: FormBuilder) {

this.createForm();

http.get(baseUrl + 'api/FlightChoice/dest_locations').subscribe(result => {
this.locations = result.json() as Location[];
console.log(this.locations);

}, error => console.error(error));

http.get(baseUrl + 'api/FlightChoice/choice').subscribe(result => {
this.flightChoice = result.json() as FlightChoice;
this.updateForm();
}, error => console.error(error));

}

createForm() {
this.form = this.fb.group({
Destination: [0],
NrPasg: [1],
TwoWays: [false],
DepartureDate: ['', Validators.required],
ReturnDate: ['', Validators.required]
});
}

updateForm() {

this.form.setValue({
Destination: this.flightChoice.DestinationId,
NrPasg: this.flightChoice.NrPasg,
TwoWays: this.flightChoice.TwoWays,
DepartureDate: this.flightChoice.DepartureDate,
ReturnDate: this.flightChoice.ReturnDate
});

}

html:

<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div>
<label for="destination">Destination:</label>
<br />
<select id="destination" formControlName="Destination">
<option *ngFor="let location of locations" value="{{ location.id }}">
{{ location.name }}
</option>
</select>
<br />
<label for="nrPasg">Number of Passengers:</label>
<br />
<input formControlName="NrPasg" type="number" id="nrPasg" value="1" />
<label for="twoWays"></label>
<br />
<select id="twoWays" formControlName="TwoWays">
<option value="false">one way</option>
<option value="true">two ways</option>
</select>
<br />
<label for="departureDate">Departure Date:</label>
<br />
<input formControlName="DepartureDate" type="date" id="departureDate" />
<br />
<label for="returnDate">Return Date:</label>
<br />
<input formControlName="ReturnDate" type="date" id="returnDate" />

</div>
<div>
<button type="submit">Search Flights</button>

</div>
</form>

我知道我可能在 CreateForm 方法中写错了,但我不确定如何分配值

最佳答案

如果您在 formGroup 上使用 setValue 但没有为该组中的每个控件传递值,则会出现此错误。例如:

let group = new FormGroup({
foo: new FormControl(''),
bar: new FormControl('')
});
group.setValue({foo: 'only foo'}); //breaks
group.setValue({foo: 'foo', bar: 'bar'}); //works

如果您真的只想更新组中的一些控件,您可以改用patchValue:

group.patchValue({foo: 'only foo, no problem!'});

setValuepatchValue 的文档 here

关于 Angular react 形式错误 : Must supply a value for form control with name:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51047540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com