MRu MRu - 2 months ago 6
AngularJS Question

How to show the most recent messages?

My task is actually trying to make a broadcast messages to show the most recent messages in a limited number only. I have found how to limit the messages in this thread. But right now i am struggling on how to show only the latest messages. I'm new in this angular, hope you can explain it in a simple way please. Thank you.

If there's 10 messages:

message 1
message 2
message 3
message 4
message 5
message 6
message 7
message 8
message 9
message 10


There result should be:

message 6
message 7
message 8
message 9
message 10


Here is the code:

<div ng-repeat="score in scores | limitTo: limit">
..........
</div>


In controller:

var limitStep = 5;
$scope.limit = limitStep;
$scope.incrementLimit = function() {
$scope.limit += limitStep;
};
$scope.decrementLimit = function() {
$scope.limit -= limitStep;
};

Answer

You can do limitTo: -limit, - helps to select from the last.

<div ng-repeat="score in scores | limitTo: -limit">

LimitTo:

Creates a new array or string containing only a specified number of elements. The elements are taken from either the beginning or the end of the source array, string or number, as specified by the value and sign (positive or negative) of limit.