AngularJS limitTo由过去的2条记录过去、AngularJS、limitTo

2023-09-13 04:44:34 作者:深拥我’

能否AngularJS 过滤器的组合订单 ,或 limitTo NG-重复 模仿 _。最后 在UnderscoreJS 解决方案

如果我理解你的问题正确,我认为这个小提琴会做。

对于这个数据:

  $ scope.items = {[排序:1,名称:第一},                【分页:1,名称:'二'},                【分页:3,名称:'三'},                {排序:4,名称:最后一个}]; 

如果您不想竟对数组进行排序,并只取数组的最后两个项目的是(如下划线的最后一个),你可以尝试一个负的限制(这将显示三,最后):

 < D​​IV NG重复=项中的项目| limitTo:-2> 
JavaScript开发教程之AngularJS过滤器

另外请注意,你可以链中的过滤器一起这样的例子排序反向的数据并采取2项(这将显示最后,第三个):

 < D​​IV NG重复=项中的项目|排序依据:'排序':真| limitTo:2> 

Can a combination of AngularJS filter, order, or limitTo for ng-repeat mimic the _.last in UnderscoreJS?

解决方案

If I'm understanding your question correctly I think this fiddle would do it.

For this data:

$scope.items = [{sort: 1, name: 'First'}, 
                {sort: 2, name: 'Second'}, 
                {sort: 3, name: 'Third'}, 
                {sort: 4, name:'Last'}];

If you don't want to actually sort the array and just take the last two items of the array as is (like underscore's last) you can try a negative limit (this would show Third, Last):

<div ng-repeat="item in items | limitTo:-2">

Also note you can chain the filters together like this example sorting the data in reverse and taking 2 items (this would show Last, Third):

<div ng-repeat="item in items | orderBy:'sort':true | limitTo:2">