javascript - 使用 ng-class (AngularJS) 搜索 JSON 对象

标签 javascript html angularjs json ng-class

我在 Controller (NotificationsController) 中定义了两个 JSON 对象。一个包含所有通知,另一个包含最新通知的 ID(最近 3 天)。

对象“通知”的格式:(t_notifications)

[{"0":"1","1":"4","2":"14-APR-16","3":"ALERT 1","ID":"1","ID_USER":"4","DATE":"14-APR-16","NOTIFICATION":"ALERT 1!"},{"0":"2","1":"1","2":"07-APR-16","3":"ALERT 2!","ID":"2","ID_USER":"1","DATE":"07-APR-16","NOTIFICATION":"ALERT 2!"},{"0":"3","1":"1","2":"13-APR-16","3":"ALERT 3!","ID":"3","ID_USER":"1","DATE":"13-APR-16","NOTIFICATION":"ALERT 3!"}]

对象“最新通知”的格式:(newest_notifications)

[{"0":"1","ID_NEWNOTIF":"1"},{"0":"3","ID_NEWNOTIF":"3"}]

我在这样的 View 中显示所有通知:

<div class="panel-body" ng-controller="NotificationsCtrl">
<table datatable="ng" class="row-border hover">
  <thead>
    <tr>
      <th><b>ID</b>&nbsp;</th>
      <th><b>ID_USER</b>&nbsp;</th>
      <th><b>DATE</b>&nbsp;</th>
      <th><b>NOTIFICATION</b>&nbsp;</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="data in t_notifications" ng-class="{selected: data.ID == **TO COMPLETE**>
      <td>{{ data.ID }}</td>
      <td>{{ data.ID_USER }}</td>
      <td>{{ data.DATE }}</td>
      <td>{{ data.NOTIFICATION }}</td>
    </tr>
  </tbody>
</table>
</div>

我想知道如何在我的表格中只选择最新的通知 - 通过 JSON 对象 newest_notifications 搜索 - 使用 ng-class?

PS:“selected”已经定义为蓝色背景色。

最佳答案

只需使用严格的过滤器

... ng-class="{'selected': (newest_notifications | filter : { ID_NEWNOTIF: data.ID } : true).length !== 0 }"

fiddle - https://jsfiddle.net/dyxf5xqj/

关于javascript - 使用 ng-class (AngularJS) 搜索 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622580/

相关文章:

javascript - 自动触发FB登录

javascript - 如何使用 ES6 在 javascript for 循环中显示当前选项卡并隐藏所有其他选项卡

javascript - 使用 Javascript 的多个日期时间之间的平均天数

html - 将导航栏品牌与 Bootstrap 垂直对齐

javascript - angularjs ng-repeat在选择选项中过滤多个字段

javascript - 将两个数组写在一起是什么意思?

javascript - 如何正确构建包含许多子级别的列表

javascript - 无法让 JavaScript 函数与下拉列表一起使用

javascript - NG 显示设置页面

angularjs - Angular JS 中的奇怪对象 - 解释?