1

I have a select input:

<select id="duration" name="duration" class="form-control select-lg"
          [(ngModel)]="championship.settings.fightDuration">
    <option *ngFor="let duration of durations"
          [selected]="championship.settings.fightDuration==duration"> 
          {{ duration }} {{ championship.settings.fightDuration==duration }}
    </option>
</select>

I have his value within championship.settings.fightDuration

I try to preselect it with:

[selected]="championship.settings.fightDuration==duration"

I in select text, I print the result of this condition, and it prints true on the element that matches the values, so it should work, but select has no selected value...

What am I missing ?

2 Answers 2

3

If someone is still having the problem selecting the default value try this.

Let's say you want the option to select has the value duration. I am assuming this option is already in the items array.

HTML

<select name="someName" [(ngModel)]="someModel">
   <option *ngFor="let key of items" [value]="key.value">{{ key.label }}</option>
</select>

TS

    class someComponent Implements OnInit {

      someModel;

      ngOnInit() { 
          this.someModel = 'duration';
          this.items = [
                            {value: 'duration', label : 'Duration'}, 
                            {value: 'other', label : 'Other'},
                            {value: 'someOther', label : 'Some Other'}
                          ];
      }

On Submit

Inside your submit function just call like this to get the selected value.

this.someModel

Hope this helps someone.

This video helped me though - https://www.youtube.com/watch?v=8ZlrORYOl_0

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

Comments

-1

You can use [ngValue] as below

<option *ngFor="let duration of durations" [ngValue]="duration"> 
      {{ duration }} {{ championship.settings.fightDuration==duration }}
</option>

Working example on stackblitz

2 Comments

It should be [ngValue]="duration".
@JuliatzindelToro I edited my answer, as ConnorsFan mentioned it should have been [ngValue]="duration"

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.