Am使用前端的Angular 8和后端的Laravel在单页应用程序上工作。我有一个具有输入文本字段和下拉列表的表单。文本输入字段上的值可以在打字稿文件上很好地捕获,但是捕获从下拉列表中选择的值时出现问题,这样我就可以在后端提交了。
〜请协助?
Create.component.html
<form #createForm=ngForm (ngSubmit)="onSubmit()">
<div class="form-group row">
<div class="col-sm-6">
<label for="formGroupExampleInput">Child SurName</label>
<input
type="text"
name="childSurName"
class="form-control"
id="childSurName"
placeholder="ChildSurName"
[(ngModel)]="form.childSurName">
</div>
<div class="col-sm-6">
<label for="child-gender">Child Gender</label>
<select class="form-control" id="childGender" name="child-gender" required>
<option value="" selected disabled> Child's Gender</option>
<option value="Male"> Male</option>
<option value="Female"> Female </option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-sm-12">
<button
type="submit"
class="btn btn-lg btn-success btn-block"
[disabled]="!createForm.valid">Save Details </button>
</div>
</div>
</form>
Create.component.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from 'src/app/Services/auth.service';
import { Router } from '@angular/router';
import { AuthCheckService } from 'src/app/Services/auth-check.service';
@Component({
selector: 'app-create',
templateUrl: './create.component.html',
styleUrls: ['./create.component.css']
})
export class CreateComponent implements OnInit {
public form = {
childSurName: null,
child-gender: null
};
public error = null;
constructor(
private Auth:AuthService,
private router: Router,
private AuthCheck : AuthCheckService)
{ }
//Submit form data to the backend via a function in he service file
onSubmit(){
this.Auth.submitFormData(this.form).subscribe(
data => console.log(data),
error => console.log(error)
);
}
ngOnInit() {
}
}
答案 0 :(得分:1)
[(ngModel)]
元素上似乎缺少 select
指令。
答案 1 :(得分:1)
将以下内容添加到<select>
[(ngModel)]="form.childGender"
为了保持一致性,还将表单的键重命名为:
public form = {
childSurName: null,
childGender: null
};
这是同样适用的stackblitz。 (请注意,在提交时,仅将表单记录到控制台)