Search code examples
angularangular5angular-ngmodelngforngmodel

Angular 5 populate selected option from database in edit form


I was able to list and add the select options as shown below:

   <select class="form-control" [(ngModel)]="feed.feed_store" name="feed_store" placeholder="Select Store">
      <option *ngFor="let feedstore of feedstores" [value]="feedstore">{{ feedstore }}</option>
    </select>

Now in my edit form I want to show the selected option from the database. Not sure to about it


Solution

  • You can set the selected attribute to the option based on a condition like so:

    <ng-container *ngIf="feed">
        <select class="form-control" [(ngModel)]="feed.feed_store" name="feed_store" placeholder="Select Store">
              <option *ngFor="let feedstore of feedstores" 
                     [value]="feedstore"
                     [selected]="feedstore === 'feedstore_from_database'">
                  {{ feedstore }}
              </option>
        </select>
    <ng-container>