Search code examples
htmlangulardropdownngmodel

Select option selected keyword not working


in my html use select options drop-down list but it's not working in my Angular project

 <td>
 <select type="text" class="form-control" [(ngModel)]="addressDetails.AddressTypeId"id="AddressTypeId" formControlName="AddressTypeId" style="height:29px; width:200px; padding:3px">
 <option disabled selected value > -- select an option -- </option>
 <option *ngFor="let addressType of addressTypeDetails" value={{addressType.AddressTypeID}}>
             {{addressType.AddressType}} </option>
 </select>
 </td>

Solution

  • You are binding ngmodel of array object, which is incorrect. I have created a sample example, please check on stackblitz I created addressDetail object to bind the with ngModel. please check the html below. Here is the component code.

    addressTypeDetails = [{AddressTypeID:1,AddressType:'first'},{AddressTypeID:2,AddressType:'second'}]
      addressDetail = {addressTypeId:0}
    

    Here is the Html Code.

     <select type="text" class="form-control" [(ngModel)]="addressDetail.addressTypeId" id="AddressTypeId"  style="height:29px; width:200px; padding:3px">
     <option > -- select an option -- </option>
     <option *ngFor="let addressType of addressTypeDetails" value={{addressType.AddressTypeID}}>
                 {{addressType.AddressType}} </option>
     </select>