gpt4 book ai didi

angular - 从 Angular 8 的下拉列表中获取选定的值

转载 作者:行者123 更新时间:2023-12-04 01:42:27 24 4
gpt4 key购买 nike

我正在开发单页应用程序,前端使用 Angular 8,后端使用 Laravel。我有一个表单,其中包含输入文本字段和一个下拉列表。文本输入字段上的值在 typescript 文件中被很好地捕获,但在捕获从下拉列表中选择的值以便我可以在后端提交时遇到问题..

~请帮忙?

创建.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>

创建.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() {
}

}

最佳答案

[(ngModel)] 指令似乎在您的 select 元素上丢失。

关于angular - 从 Angular 8 的下拉列表中获取选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56845317/

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