reza
reza

Reputation: 6358

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"?

Upvotes: 0

Views: 1104

Answers (2)

Charley Erd
Charley Erd

Reputation: 103

Another alternative, to use a defined constant within your .ts file of the component, and initialize it's that variable's value in the constructor.

Upvotes: 0

Jota.Toledo
Jota.Toledo

Reputation: 28434

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

Upvotes: 2

Related Questions