【问题标题】:Angular UI, Unique not workingAngular UI,独特的不起作用
【发布时间】:2014-07-17 20:23:55
【问题描述】:

所以我试图通过使用角度唯一模块来过滤我的 ng-repeat 指令。但我有点迷路了,我按照添加脚本的确切顺序...在控制台中我收到一个错误, '没有模块:ui.utils'..

这是我的 Plunker:http://plnkr.co/edit/cKJgtGyYeQdQCyARXG7j?p=preview

从此代码:

<ul>
 <li>nokia</li>
 <li>nokia</li>
 <li>samsung</li>
</ul>

我应该得到-unique-这个代码:

<ul>
 <li>nokia</li>
 <li>samsung</li>
</ul>

这里的一些好人已经通过使用 jQuery 过滤器帮助了我,但我认为这是更好的方法。 任何帮助都非常感谢..

【问题讨论】:

  • 问题已经找到,唯一的:名字,应该是唯一的:'name'...愚蠢的错误:/
  • 您的演示似乎也在过时版本的 AngularUI 上。你可能想关闭问题大声笑。
  • 我会保留它,只是为了可能有同样问题的其他人,AngularUI 结构对于像我这样的新手来说真的很棘手:)
  • 请在下方提供答案并接受或删除您的问题。

标签: angular-ui angularjs-ng-repeat


【解决方案1】:

您可以在 angular.filter 模块中使用 'unique'(aliases: uniq) 过滤器 (https://github.com/a8m/angular-filter)

用法:colection | uniq: 'property'
您可以按嵌套属性过滤到:colection | uniq: 'property.nested_property'

所以你可以做这样的事情..

function MainController ($scope) {
 $scope.orders = [
  { id:1, customer: { name: 'foo', id: 10 } },
  { id:2, customer: { name: 'bar', id: 20 } },
  { id:3, customer: { name: 'foo', id: 10 } },
  { id:4, customer: { name: 'bar', id: 20 } },
  { id:5, customer: { name: 'baz', id: 30 } },
 ];
}

HTML:我们按客户 ID 过滤,即删除重复客户

<th>All customers list: </th>
<tr ng-repeat="order in orders | unique: 'customer.id'" >
   <td> {{ order.customer.name }} , {{ order.customer.id }} </td>
</tr>

结果: 所有客户名单:
富 10
酒吧 20
baz 30

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 2014-08-14
    • 2016-03-22
    • 1970-01-01
    • 2013-05-08
    • 2018-05-11
    • 2016-01-01
    相关资源
    最近更新 更多