Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

hide an option based on some condition

I want to hide an option from ng-option based on some condition. I tried using [hidden] attribute on ng-option but it didn’t work.

<ng-select [hideSelected]="true" [loading]="isLoading" [multiple]="true"
                           [(ngModel)]="selectedIds" [closeOnSelect]="false"
                           #selectedNames="ngModel" required
                           placeholder="{{'Names' | translate}}" [clearable]="true">
                    <ng-option *ngFor="let name of names" [value]="name.id">{{name.Label}}</ng-option>
</ng-select>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use ng-container apply *ngFor to iterate over names and *ngIf on ng-option to apply the condition.

This is needed because it’s not allowed to use multiple structural directives (such as *ngIf, *ngFor, etc) on an element.

<ng-container *ngFor="let name of names">
    <ng-option *ngIf="someCondition"  [value]="name.id">{{name.Label}}</ng-option>
</ng-container>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading