Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 8 drop down selected option not showing, how come?

This question has been asked frequently. My situation is apparently different from all the others as I can't find a solution for it from the existing answers.

I have this code:

<form (ngSubmit)="getExceptions(f)" #f="ngForm">
          <div class="row">
            <div class="form-group col-xs-3 col-sm-3">
              <label class="col-form-label" for="aantal">Aantal meldingen per pagina?</label>
              <select name="selectedQuantity" id="aantal" class="form-control" ngModel>
                <option *ngFor="let option of options" [value]="option">{{option}}</option>

              </select>
            </div>
.
.
.
</form>

options is defined as:

private options: string[] = ['10', '20', '50'];

I would like to show "10" as the default selected value but whatever I do the drop down keeps showing a blank for the selected value. Clicking the drop down shows the values 10, 20 and 30. So it is filled properly.

What did I try:

<option *ngFor="let option of options" [value]="option" [selected]="option==10">{{option}}
</option>

and

<option [value]="10" [selected]="true == true">10</option>
<option [value]="20">20</option>
<option [value]="50">50</option>

and

<option [value]="10" selected>10</option>
<option [value]="20">20</option>
<option [value]="50">50</option>

It has something to do with the binding because if I remove the 'ngModel' from the select tag it shows the value I want to use as the default value (10). Sort of. I can't read the selected value anymore but the default value is showing.

I have never done anything with plunkr but will try to get an example working there and then paste a link to that in here.

like image 300
Werner van Mook Avatar asked Dec 12 '19 08:12

Werner van Mook


People also ask

How do I get the selected value of dropdown in TypeScript?

First, create the template variables for dropdown using #teams . Use the event binding to listen for the change event and link with the onSelect method. The onSelect method receives the reference variable and takes the value of the dropdown element. Next, create the onSelected(value) method in the TypeScript file.


2 Answers

Try setting ngModel like this:

.ts

  private options: string[] = ["10", "20", "50"];
  selectedQuantity = "10";

.html

<select name="selectedQuantity" id="aantal" class="form-control" [(ngModel)]="selectedQuantity">
    <option *ngFor="let option of options" [value]="option" >{{option}}</option>
</select>

Working Demo

like image 65
Adrita Sharma Avatar answered Oct 26 '22 23:10

Adrita Sharma


Since We just want a placeholder for our select tag why not just try a different approach.

Using disabled attribute somehow makes angular ignore it. Which then perfectly works as placeholder.

<select class="form-select" name="NAME" ngModel>
   <option value="" disabled selected>Select text</option>
   <option value="1">One</option>
   <option value="2">Two</option>
   <option value="3">Three</option>
</select>
like image 27
Aakash Tandew Avatar answered Oct 27 '22 00:10

Aakash Tandew