简体   繁体   English

如何使用带有常量和 ngModel 的 ngIf 条件

[英]How to use ngIf condition with a constant and a ngModel

I have the following html file.我有以下 html 文件。

<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"?如果我想比较 {{selectedMetric}} 和像“option 1”这样的字符串常量,我的 ngIf 指令的正确语法是什么?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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