0

I'm trying to get a value from dropdown list. but the value doesn't parse from .html to .ts. any help with that, please.

.html file

<mat-card>
    <form (submit)="onAddName(nameForm)" #nameForm="ngForm">

        <mat-form-field appearance="fill">
            <mat-label>select one</mat-label>
            <mat-select name="name" id="name">
                <mat-option value="NAME_01">NAME 01</mat-option>
                <mat-option value="NAME_02">NAME 02</mat-option>
                <mat-option value="NAME_03">NAME 03</mat-option>
            </mat-select>
        </mat-form-field>
    
  </form>
</mat-card>

.ts file

 onAddName(form: NgForm) {
    if (form.invalid) {
      return;
    }
    this.nameService.addname(
      form.value.name, 
      );
    form.resetForm();
  }

in .ts file, I've used form.value.<name-from-html> to get other form values and they worked. only dropdown list is not working. any idea how to slove this.

1

4 Answers 4

1

You need to put ngModelangular attribute to access its value

<mat-select name="name" id="name" ngModel>// <--
            <mat-option value="NAME_01">NAME 01</mat-option>
            <mat-option value="NAME_02">NAME 02</mat-option>
            <mat-option value="NAME_03">NAME 03</mat-option>
        </mat-select>

Here's the working Plunker

Sign up to request clarification or add additional context in comments.

Comments

1

you can check out the link for getting value from the dropdown list while selecting the dropdown list selected item for that we will get the value for the respective item.

Get selected value from a drop-down list in Angular

Comments

0

Use 2 way binding put ngModel on your select tag

Comments

-1
    <mat-form-field appearance="fill">
        <mat-label>select one</mat-label>
           <mat-select [(value)]="selected">
            <mat-option value="NAME_01">NAME 01</mat-option>
            <mat-option value="NAME_02">NAME 02</mat-option>
            <mat-option value="NAME_03">NAME 03</mat-option>
        </mat-select>
    </mat-form-field>

.ts file

1 Comment

Would you care to edit your answer and explain the code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.