0

I am setting value of select option in a loop . Have a look at the code

<div *ngFor="let sport of sport_data;" class="row">
  <select [(ngModel)]="sports.value" class="form-control">
      <option value="football">Sports</option>
      <option value="cricket">Cricket</option>
  </select>
</div>

Here sport_data is the array of list of sports . Its not working fine . Is their any other way to set default value of select element?

1 Answer 1

1

Should the select have a binding to the "sport" variable of the *ngFor? In that case it seems that you have a typo, as you bind the ngModel to "sports" and not "sport". The code should look like this:

<div *ngFor="let sport of sport_data;" class="row">
  <select [(ngModel)]="sport.value" class="form-control">
      <option value="football">Sports</option>
      <option value="cricket">Cricket</option>
  </select>
</div>

As you asked for it, a different approach could be to use the (change) event on the select to set the value and the [selected] binding for the option to be selected.

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

3 Comments

Here I am using ngmodel to set default value and sport.value returns value like football and cricket . Even after correcting my typo error its not working .
@MOHIT I made a small plunkr with your code, see link. Is this what you want to achieve? I.e. multiple selection boxes for each field of your sport_data?
Yes thats what i was trying to achieve . Thanks

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.