2

vatCodeList is an error with string codes.

Example: ['34u' , '23' ,'tt']

Need to set the selected value there .

<select class="custom-select" formControlName="vatCode">             
            <option *ngFor="let i of vatCodeList">{{i}}</option>          
</select>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Adikari Nadeesha
  • 322
  • 3
  • 12
  • 5
    Possible duplicate of [Binding select element to object in Angular 2](https://stackoverflow.com/questions/35945001/binding-select-element-to-object-in-angular-2) – s.alem Jul 26 '17 at 11:11

3 Answers3

1

Inside your *.component.ts

public vatCode: any;

Inside your *.component.ts you can set the value of vatCode to one of the values contained within vatCodeList, this will update the selected value.

Inside the *.component.html

<select class="custom-select" formControlName="vatCode" [(ngModel)]="vatCode">             
  <option *ngFor="let i of vatCodeList">{{i}}</option>          
</select>
Wesley Coetzee
  • 4,768
  • 3
  • 27
  • 45
0

You can bind the value property like this

<option [value]="i" *ngFor="let i of vatCodeList">{{ i }}</option>
Osman Cea
  • 1,467
  • 9
  • 18
0

You can try to put an expression to the option tag to make an option selected

<select class="custom-select" formControlName="vatCode">             
            <option *ngFor="let i of vatCodeList" {{i == vatCode?'selected':'' }}>{{i}}</option>          
</select>

The variable should reference the value of the InputControl. Using reactive forms it would be easy to extract the value and put it into expression.


The easiest way to bind the element to the model with ngModel but you can check if this solution helps.

Roman C
  • 49,761
  • 33
  • 66
  • 176