首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带if的角5 ngClass

带if的角5 ngClass
EN

Stack Overflow用户
提问于 2018-03-28 10:10:34
回答 3查看 1.4K关注 0票数 2

这是表中一行的代码

代码语言:javascript
复制
  <tr class="user-item" *ngFor="let device of group.devices" (click)="$event.stopPropagation()" ngClass="device.onlineState == 'Offline' ? 'highlight' : 'highlight2'">
    <td class="qkFix">{{ device.deviceId }}</td>
    <td class="qkFix">{{ device.alias }}</td>
    <td class="qkFix" *ngIf="device.onlineState == 'Online'" ngClass="colorOnline">{{ device.onlineState }}
      <span class="dotOnline"></span>
    </td>
    <td class="qkFix" *ngIf="device.onlineState == 'Offline'" ngClass="colorOffline">{{ device.onlineState }}
      <span class="dotOffline"></span>
    </td>
    <td class="qkFix">
      <button type="button" class="btn btn-default btn-xs" (click)="$event.stopPropagation(); showDevice(device); editDeviceDialog()">
        <i class="material-icons">{{ (auth.hasPermissions(['update-devices'], true)) ? 'edit' : 'open_in_new'}}</i>
      </button>
      <button [disabled]="!(auth.hasPermissions(['delete-devices'], true))" type="button" class="btn btn-default btn-xs" (click)="$event.stopPropagation();deleteDevice(device)">
        <i *ngIf="(auth.hasPermissions(['delete-devices'], true))" class="material-icons" (click)="$event.stopPropagation();deleteDevice(device)">delete</i>
        <i *ngIf="!(auth.hasPermissions(['delete-devices'], true))" class="material-icons" (click)="$event.stopPropagation()">delete</i>
      </button>
      <button *ngIf="device.onlineState == 'Online'" [disabled]="!(auth.hasPermissions(['remote-control'], true))" type="button" class="btn btn-default btn-xs" (click)="$event.stopPropagation(); remoteSession(device)">
        <i *ngIf="(auth.hasPermissions(['remote-control'], true))" (click)="$event.stopPropagation();remoteSession(device)" class="material-icons">swap_horiz</i>
        <i *ngIf="!(auth.hasPermissions(['remote-control'], true))" (click)="$event.stopPropagation()" class="material-icons">swap_horiz</i>
      </button>
    </td>
  </tr>

专用ngClass码

代码语言:javascript
复制
ngClass="device.onlineState == 'Offline' ? 'highlight' : 'highlight2'"

,我希望行具有类 highlight ,如果该设备的onlineState是脱机的。 device.onlineState either returns Online or Offline如果onlineState是联机的,就不应该有一个类.

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-28 10:13:48

尝试将ngClass放在括号中,即[ngClass]想了解更多细节

答案:

代码语言:javascript
复制
 [ngClass]="device.onlineState == 'Offline' ? 'highlight' : 'highlight2'"

如果要添加multiple classes,可以这样做:

option1:

代码语言:javascript
复制
 [ngClass]="condition ? 'class1 class2 class3' : 'class4 class5 class6'"

Option2:

代码语言:javascript
复制
[ngClass]="{'class1': condition1, 'class2': Condition2}"
票数 4
EN

Stack Overflow用户

发布于 2018-03-28 10:15:44

你可以这样做:

代码语言:javascript
复制
  [class.highlight]="device.onlineState == 'Offline'"

您已经必须执行[ngClass]而不是ngClass来允许表达式的执行。

票数 2
EN

Stack Overflow用户

发布于 2018-03-28 10:17:40

ngClass是一个指令,它以类的名称作为属性值和条件作为值,接受对象作为参数,例如:

代码语言:javascript
复制
[ngClass]="{'highlight': device.onlineState === 'Offline'}"

您可以添加这样的多个类:

代码语言:javascript
复制
[ngClass]="{'highlight other-class': device.onlineState === 'Offline'}"

代码语言:javascript
复制
[ngClass]="{'highlight': device.onlineState === 'Offline', 'other-class': condition}"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49531743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档