Fetching Selected value from a dropdown list in Angular 8

和自甴很熟 提交于 2021-02-19 06:15:08

问题


Am working on a Single Page Application using Angular 8 on the frontend and Laravel on the backend. I have got a form which has input text field and a dropdown list. The values on the text input fields are being captured well on the typescript file but am having a problem capturing the value selected from the drop down list so that I can submit on the backend..

~ Kindly assist?

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

}

回答1:


Add the following to the <select>

[(ngModel)]="form.childGender"

Also for consistency rename the key of your form to:

public form = {
   childSurName: null,
   childGender: null
};

Here's a working stackblitz for the same. (Note that on submit only the form is being logged to the console)




回答2:


[(ngModel)] directive seems to be missing on your select element.



来源:https://stackoverflow.com/questions/56845317/fetching-selected-value-from-a-dropdown-list-in-angular-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!