I am developing inbox for messages with AngularJs, and I came across one issue: I want to show only last element within ng-repeat
. I did some research, and found out that the code below should work.
<div class="inbox" ng-repeat="recipient in messages">
<ul>
<div>
<span> {{recipient.$id}} <br> </span>
<li class="inboxItem">
<span ng-repeat="messages in recipient"> {{messages.sender}} {{messages.message}} <br></span>
</li>
</div>
</ul>
</div>
However, it does not. Can you guys tell me what am I doing wrong? I thought that array.length-1
should limit ng-repeat to show only the last object in an array.
Thanks!
You could use
limitTo
filter with-1
Example :
See fiddle
But if you are showing only one element, maybe you want to get rid of the
ng-repeat
... :use angularjs
$last
inng-repeat
. here is the doc<span ng-show="$last">{{n}}</span>
when its last repeat$last
will betrue
otherwise itsfalse
so you can useng-show
orng-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:
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.demo
Something like this should work.