Skip to content
Advertisement

How to use ngIf condition with a constant and a ngModel

I have the following html file.

<p-selectButton
        [options]="metrics"
        [(ngModel)]="selectedMetric"
        (onChange)="onChange($event)"
></p-selectButton>
<p>Selected Metric: {{selectedMetric}}</p>
<div *ngIf="selectedMetric"==="Metric 1">Selected first </div>

The above is obviously incorrect. What is the correct syntax for my ngIf directive if I want to compare {{selectedMetric}} and a string constant like “option 1”?

Advertisement

Answer

<div *ngIf="selectedMetric==='Metric 1'">Selected first </div> should to the trick to compare a variable against a literal string

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement