JenuRudan
JenuRudan

Reputation: 545

Angular 4 - ngfor with ngif error

I'm developing anuglar 4 project, where the data is fetched in the component Loop through it in the select html, but i want to mark specific option as selected according to it's name

Code

<select class="form-control" id="exampleSelect1">
    <option  *ngFor="let field of fields" *ngIf="field.name == 'Select me'; then selected='true'">{{field.name}}</option>
</select>

but it's not working when using ngif with ngfor

Error

Error: Template parse errors:
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with *

Upvotes: 0

Views: 398

Answers (1)

JayDeeEss
JayDeeEss

Reputation: 1083

instead of ngIf you can use [selected]="field.name==='Select me'" property for selecting the option

<select class="form-control" id="exampleSelect1">
        <option  *ngFor="let field of fields" [selected]="field.name==='Select me'">{{field.name}}</option>
    </select>

Upvotes: 1

Related Questions