简体   繁体   中英

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;
};

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.

You can do this by slicing the values,

 <ul ng-repeat="strVal in arrVal.slice(5, 10)">
    <li>{{strVal}}</li>
  </ul>

Or by using angular Feature LimitTo

<div ng-repeat="strVal in arrVal| limitTo: 10 | limitTo: -5">{{strVal }}</div>

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM