如何在过滤后更改数组变量 - Angular Ui 分页

How to change array variable after filter - Angular Ui Pagination

我有这个codepen。我需要在过滤器之后更改 'totalItems' 的值。我正在尝试在过滤器中这样做:

scope.totalItems = scope.filteredlist.length;

但值没有改变。我读到我应该做这样的事情 plnkr(在我的控制器中再次调用过滤器),但我不知道如何使它适应我的代码。

PS:要测试过滤器,请尝试在搜索框中键入内容,例如 'o'.

您可以使用语法 item in items | filter:x as results

将过滤后的数组结果存储在变量中

variable in expression as alias_expression – You can also provide an optional ?>alias expression which will then store the intermediate results of the repeater >after the filters have been applied. Typically this is used to render a special >message when a filter is active on the repeater, but the filtered result set is >empty.

For example: item in items | filter:x as results will store the fragment of the >repeated items as results, but only after the items have been processed through >the filter.

然后就可以用{{results.length}}得到过滤结果的长度了

https://code.angularjs.org/1.3.10/docs/api/ng/directive/ngRepeat


在更详细地查看您的笔后,您只需在代码中进行一行更改即可获得范围内的 totalItems 值。在您的分页过滤器中,您正在做

scope.totalItems = scope.filteredlist.length;

但是你想要的长度实际上是 input 所以如果你把它改成这个,那么 totalItems 就会有你期望的值

scope.totalItems = input.length;

我不太喜欢这个解决方案,因为您最终会污染分页过滤器中的其他一些范围,但它会为您提供答案。 (我只是需要更多的时间来想出一个“更干净”的解决方案)