Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular default option selected attribute not working as expected

Tags:

angular

I would like to select the first option from the dropdown list. However, when I set [attr.selected]="index == 1" or [attr.selected]="index == 0" its not working but if I change the index value condition to [attr.selected]="index == 2", it works. Although the index starting from 0 to 2 is present.

<select formControlName="segmentforOverlapOne" class="form-control overlap-segment-1">
  <option [attr.selected]="index == 1" *ngFor="let segment of segmentations; let index=index;" [ngValue]="index">{{segment.name}}</option>
</select>

Below is the segmentations array:

[
  {
    "name": "name 1",
    "columns": [
      {
        "index": 0,
        ...
      }
    ],
  },
  {
    "name": "name 2",
    "columns": [
      {
        "index": 1,
        ...
      }
    ],
  },
  {
    "name": "name 3",
    "columns": [
      {
        "index": 2,
        ...
      }
    ]
  }
]
like image 861
Rahul Dagli Avatar asked Dec 12 '25 13:12

Rahul Dagli


1 Answers

try like this :

<select formControlName="segmentforOverlapOne" class="form-control overlap-segment-1"> 
    <option *ngFor="let segment of segmentations; let i = index" [attr.value]="i" [attr.selected]="i == 0 ? true : null">{{segment.name}}</option>
</select>
like image 192
Chandru Avatar answered Dec 14 '25 05:12

Chandru