【发布时间】:2020-05-22 12:34:18
【问题描述】:
在学习 Angular 时,我遇到了一个问题。
我有一个使用响应式方法的表单。
我有一个数组“模型”,其中包含每个“模型”的“价格”
我希望当我选择“模型”时,它应该给我它的“价格”,当我验证表单时,我会在 console.log (this.form.value )
这是我的 HTML:
<form [formGroup]="factureForm" (ngSubmit)="onSubmitForm()">
<select formControlName="model">
<option *ngFor="let model of models">{{ model.model }}</option>
</select>
<select formControlName="price">
<option *ngFor="let model of models">{{ model.price }}</option>
</select>
<button type="submit">Submit</button>
</form>
这是我的 TS:
import { Component, OnInit } from "@angular/core";
import { FormsModule, FormGroup, FormBuilder } from "@angular/forms";
@Component({
selector: "app-relational-data",
templateUrl: "./relational-data.component.html",
styleUrls: ["./relational-data.component.css"],
})
export class RelationalDataComponent implements OnInit {
factureForm: FormGroup;
models = [
{
model: "Model 1",
price: 20,
},
{
model: "Model 2",
price: 50,
},
];
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.initFactureForm();
}
initFactureForm() {
this.factureForm = this.formBuilder.group({
model: [""],
price: [""],
});
}
onSubmitForm() {
const newFacture = this.factureForm.value;
console.log(newFacture);
}
}
我迷路了。 提前谢谢你:)
【问题讨论】:
标签: angular angular-reactive-forms