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

时间:2022-05-28 19:41:24

I need a length of filtered bookshelf here are what i have.

我需要一定长度的过滤书架,这就是我所拥有的。

Models:
1. Book: id, category_id
2. Bookshelf: id, book_id, read_book

型号: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

Maybe i can filtered it in controller some how. For example:

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

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

Description of logic

逻辑描述

I = current_user
50 books with different categories are in DB. 30 of them I have on my bookshelf and 10 of 30 has category_id:5, and 3 of this 10 are readread_book:true. So in my case filteredBookshelf should be 3

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

1 个解决方案

#1


1  

You can have something like

你可以拥有类似的东西

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

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

{{l}} {{p.length}} // 3 1

In your case

在你的情况下

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

{{ filtered.length }}

{{filtered.length}}

Example in plkr: http://plnkr.co/edit/ye6ARaJdBxJVyCXc4cqR?p=info

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

#1


1  

You can have something like

你可以拥有类似的东西

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

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

{{l}} {{p.length}} // 3 1

In your case

在你的情况下

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

{{ filtered.length }}

{{filtered.length}}

Example in plkr: http://plnkr.co/edit/ye6ARaJdBxJVyCXc4cqR?p=info

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