0

I have following template

  <select  [(ngModel)] = "selectedLocation">
    <option *ngFor="let location of allLocationNames" [ngValue]="location">{{location.name}}</option>
  </select>

The component has following ngOnInit

export class TestComponent implements OnInit {
  @Input() public guest: Guest;
  public allLocationNames: Location[] = []
  public selectedLocation: Location;


  constructor(private apiService: ApiService) {
  }

  ngOnInit() {
    this.allLocationNames = this.apiService.allLocationNames;
    this.selectedLocation = this.guest.location;
  }
}

I would like the default selected element to be the one set in this.selectedLocation when the component loads. Right now the selected element in the drop down list seems to be a blank entry.

I am on Angular 5

isADon
  • 3,433
  • 11
  • 35
  • 49
  • this is a expected behavior from Angular, It does not know what will be the value of **selectedLocation** by default, you should give the default value in the beginning ! – SHOHIL SETHIA Nov 08 '17 at 13:55
  • I see that your `location` objects have a `name` property. Do they have an `id` property? You could bind to that property instead of binding to the object itself (e.g. `[ngValue]="location.id"` and `[(ngModel)]="selectedLocationId"`). Or you could bind to the name, if you are sure that they are unique. – ConnorsFan Nov 08 '17 at 13:55
  • initialize **this.selectedLocation** which you used as **ngModel** for 2-way binding in typescript before using it ! Since if you have not initialized it, Angular does not know what to show as the first attribute in dropdown ! – SHOHIL SETHIA Nov 08 '17 at 14:01
  • I believe I do initialize it in ngOnInit() ? – isADon Nov 08 '17 at 14:02
  • Please post your full code, As only scenario when dropdown shows empty item in first place is not initializing the ngModel attribute – SHOHIL SETHIA Nov 08 '17 at 14:04
  • @SHOHILSETHIA I edited it – isADon Nov 08 '17 at 14:09
  • Hey did either answer help you, or do you need further assistance? :) – AT82 Nov 18 '17 at 08:17

3 Answers3

0

Your guest.location is a string, whereas each location is an object. If you in this case want to capture the whole object, i.e use [ngValue]="location", you'd want to compare the string value you are getting in guest.location and assign the object that matches, to your selectedLocation, instead of just having a string value there.

So it can be done like:

ngOnInit() {
  this.allLocationNames =  this.apiService.allLocationNames;
  this.selectedLocation = 
       this.allLocationNames.find(x => x.name === this.guest.location)
}

now you have the whole object in your selectedLocation.

DEMO

AT82
  • 71,416
  • 24
  • 140
  • 167
0

check once...

    <option value="undefined" disabled selected hidden>select</option>
    <option *ngFor="let example2 of example2s" value="{{example2.value}}">{{example.exampleName}}</option>
</select>

Thanks.

Arun Kumar
  • 21
  • 3
-1

You need to bind to selected property of option element. Just use [selected]= "location === selectedLocation" in your options element and your are good to go.

<option *ngFor="let location of arrayList" [selected]="location === selectedItem" >{{location.name}}</option>
Ankit Kapoor
  • 1,615
  • 12
  • 18