1

Below My code has one form with few inputs. I want get all the values on submit. Able to get all the element values using ngModel. How can I get select option selected value.

 <form #f="ngForm" (ngSubmit) = "saveData(f)">
  <fieldset>
   <legend>Your Details:</legend>
   <label>Name: <input type="text" name="name" size="30" maxlength="100" [(ngModel)] = "name"></label><br />
   <label>Email: <input type="email" name="email" size="30" maxlength="100" [(ngModel)] = "email"></label><br />
   <label>Password: <input type="password" name="password" size="30" maxlength="100" [(ngModel)] = "password"></label><br />
   <select>
     <option *ngFor = "let combo of combos" value={{combo}}>{{combo}}
     </option>
   </select>
 <input type="submit" value="Save"/>
</fieldset><br />
</form>

3 Answers 3

2

I think it should be [value]="combo"

<select #selCombo>
  <option *ngFor = "let combo of combos" [value]="combo">{{combo}}
  </option>
</select>

Then get selected value in HTML side (if you don't want to write in component)

<div>{{selCombo.value}}</div>

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

1 Comment

I want selected answer on submit.. I am calling saveData method with #f form varaible
0

problem was not setting the name property.

<select [(ngModel)]="gender" name="gender">
   <option *ngFor = "let combo of combos" [value]=combo>{{combo}}
   </option>
</select>

Comments

0

Use [(ngModel)] to bind selected value with variable OR Model as below:

<select [(ngModel)]="model.combo">
 <option *ngFor = "let combo of combos" value={{combo}}>{{combo}}
 </option>
</select>

OR

let comboValue : any;

<select [(ngModel)]="comboValue">
 <option *ngFor = "let combo of combos" value={{combo}}>{{combo}}
 </option>
</select>

2 Comments

Cant I include it through form variable that is #f
I think you not need #f, if we go with any event action then we can use it like focus event and other

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.