ng-repeat show only last element

感情迁移 提交于 2019-12-03 10:21:00

use angularjs $last in ng-repeat. here is the doc

<div ng-repeat="n in data track by $index">
   <span ng-show="$last">{{n}}</span>
</div>

<span ng-show="$last">{{n}}</span> when its last repeat $last will be true otherwise its false so you can use ng-show or ng-if with $last.

here is a sample demo


UPDATE I think no need of ng-repeat there since you need to show the last element of the array (this is same as @Michael P. Bazos's answer), to do that:

$scope.data = [42, 42, 43, 43, 50];

print the last value as : {{ data[data.length-1] }}

here is the demo

but if you really need to do with ng-repeat do as this

[data[data.length-1]] create an array only with last element.

<div ng-repeat="n in [data[data.length-1]] track by $index">
    <span>{{n}}</span>
</div>

demo

You could use limitTo filter with -1

Example :

<div ng-repeat="friend in friends | limitTo: -1">
    {{friend.name}}
</div>

See fiddle

But if you are showing only one element, maybe you want to get rid of the ng-repeat... :

<div>
    {{friends[friends.length - 1].name}}
</div>

Something like this should work.

<div ng-repeat="something in vm.somethings">
  <span ng-show="$last">{{ something.value }}</span>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!