简体   繁体   中英

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

<div *ngIf="selectedMetric==='Metric 1'">Selected first </div>应该使用将变量与文字字符串进行比较的技巧

另一种选择,在组件的 .ts 文件中使用定义的常量,并在构造函数中初始化该变量的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM