[英]How to use ngIf condition with a constant and a ngModel
我有以下 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>
以上顯然是錯誤的。 如果我想比較 {{selectedMetric}} 和像“option 1”這樣的字符串常量,我的 ngIf 指令的正確語法是什么?
<div *ngIf="selectedMetric==='Metric 1'">Selected first </div>
應該使用將變量與文字字符串進行比較的技巧
另一種選擇,在組件的 .ts 文件中使用定義的常量,並在構造函數中初始化該變量的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.