如何获取ng-repeat中的项目长度或使用带过滤器的ng-init

时间:2015-07-20 16:37:40

标签: angularjs

我需要一长度的过滤bookshelf这就是我所拥有的。

型号:
1.书:id,category_id
2. Bookshelf:id,book_id,read_book

<div ng-repeat = 'book in filteredBooks = (books | filter: {category_id: 5})'>
  <div ng-init = 'filteredBookshelf = (bookshelf | filter: {read_book:true}).length'> 
    {{filteredBookshelf}}
  </div>
</div>

JSfiddle

也许我可以在控制器中过滤一些方法。例如:

$scope.bookshelfLength = $filter('filter')(bookshelf, {book.category_id: 5, read_book: true}).length

逻辑描述

I = current_user
DB中有50本不同类别的书籍。我在书架上有30个,30个中有10个有category_id:5,其中3个是read_book:true。所以在我的情况下filteredBookshelf应该是3

1 个答案:

答案 0 :(得分:1)

您可以拥有类似

的内容

len = [1,2,3,4,5,6,7,8]

<div ng-repeat="l in len | filter:3 as p"> {{ l }}{{ p.length }}</div> // 3 1

在你的情况下

<div ng-repeat = 'book in filteredBooks = (books | filter: {category_id: 5}) as filtered'>

{{ filtered.length }}

plkr中的示例:http://plnkr.co/edit/ye6ARaJdBxJVyCXc4cqR?p=info