Using ngIf and ngFor in option

All we need is an easy explanation of the problem, so here it is.

I want to use ngIf and ngFor in one line. I know it is not possible but is there any other method to do this?

Here is my code:

<option *ngIf="tmpLanguage.id!=languages.id" 
        *ngFor="let tmpLanguage of languages" [ngValue]="tmpLanguage.id">
     {{tmpLanguage.identificatie}}
</option>

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Only one structural directive is allowed on one element at a time.

As a workaround you can use <ng-container> which is not stamped to the DOM

<ng-container *ngFor="let tmpLanguage of languages">
  <option *ngIf="tmpLanguage.id!=languages.id"  [ngValue]="tmpLanguage.id" >{{tmpLanguage.identificatie}}</option>
</ng-container>

Method 2

<ng-container *ngFor="let tmpLanguage of languages">
  <option *ngIf="tmpLanguage.id!=languages.id" [ngValue]="tmpLanguage.id" >
    {{tmpLanguage.identificatie}}
  </option>
</ng-container>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply