【问题标题】:How to hide element in angular if one of the two condition is true如果两个条件之一为真,如何以角度隐藏元素
【发布时间】:2021-12-16 14:49:55
【问题描述】:

如果条件之一为真,如何以角度隐藏元素。

我尝试使用 *ngIf="productID == category.Lane || productID == category.Val"。它不起作用。

 <label>ProductID</label>
      <ng-select
        appearance="outline"
        
        formControlName="productId"
        [clearable]="false"
        [searchable]="false"
        [(ngModel)]="productID"
        placeholder="Select an option"
      >
        <ng-option *ngFor="let product of products | orderBy: 'name'" value="{{ product.id }}">{{ product.name }}</ng-option>
      </ng-select>
      
      <div class="row" *ngIf="productID == category.Lane || productID == category.Val">
      <div class="col">
      <label>Promotion</label>
     </div>

【问题讨论】:

  • 你想在这里隐藏什么?你试过'ngIf else'吗?

标签: javascript html angular typescript


【解决方案1】:

在这种情况下:

*ngIf="productID == category.Lane || productID == category.Val"

您的意思是,如果第一个或第二个条件为真,则显示该元素。如果你想隐藏它,那么你应该使用相反的条件:

*ngIf="!(productID == category.Lane || productID == category.Val)"

【讨论】:

    【解决方案2】:

    否则,你可以使用 hidden 属性而不是使用 *ngIf 条件

    <div class="row" [hidden]="productID == category.Lane || productID == category.Val">
    

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 2018-04-23
      • 2013-04-19
      • 2017-09-23
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 2018-12-06
      • 2021-03-14
      相关资源
      最近更新 更多