0

here is my code

<select class="form-control input-transparent" id="select-field" name="address" ngModel required>
     <optgroup *ngFor="let wallet of wallets" label="{{wallet.label}}">
         <option value="{{ wallet.address }}" [selected]="wallet.address == wallets[0].address">{{ wallet.address }}</option>
     </optgroup>
</select>

I want to select first option as selected but still on default value was selected

Hamid Aijaz
  • 47
  • 2
  • 7

2 Answers2

2

selected is not supported with ngModel

You can use this sample code. I have created a demo on stackblitz. I hope this will help/guide to you/others.

HTML Code

<select class="form-control input-transparent" id="select-field" name="address" required [(ngModel)]="address">
    <optgroup *ngFor="let wallet of wallets;" label="{{wallet.label}}">
        <option [value]="wallet.id">{{ wallet.address }}</option>
    </optgroup>
</select>

.ts Code

    address:number=1;
    wallets= [{
        id: 1,
        address: 'address1',
        label: 'Label 1'
    }, {
        id: 2,
        address: 'address 2',
        label: 'Label 2'
   }]
Krishna Rathore
  • 9,389
  • 5
  • 24
  • 48
  • thanks man ! working fine . whatever you are binding to [value] , you should set the value of the ngModel variable as one of its value . example if you are doing like this , then you can do in the component as address:string='address1'; – AhammadaliPK Sep 05 '18 at 07:22
0

Use the <selected> tags property value and set it to your desired value.

<select value="wallets[0].address">

You can set a pre-selected option in a drop-down list by using the value attribute for the tag, i.e. , but it's not valid in the W3C validator. – Apostle May 20 '15 at 15:40 How can I set the default value for an HTML <select> element?

Daddelbob
  • 406
  • 4
  • 13