0

I have an ng-repeat list that starts with 10 items. I would like to add new items at the end of the list (bottom) If I currently run the function it will add items to the top of my list.

I know that you can add items with .push() and .unshift() but this is not working with the code I use.

This is my code (I use a code snippet from Pubnub to fetch history items):

$scope.messages = [];

$scope.loadMore = function() {

    var timeToken = $('.lastMessage').attr('timetoken');
    console.log(timeToken);

    // Populate message history
    PubNub.ngHistory({
        start: timeToken,
        channel: $scope.channel,
        count: 5,
    });

};

html:

<ion-item ng-repeat="message in messages track by $index">
Bhavesh Jariwala
  • 885
  • 8
  • 27
user1242574
  • 1,257
  • 3
  • 12
  • 29
  • They're showing at the top because that's where pubNub's ngHistory is putting them. Their docs suggest you can use a 'reverse' param: http://www.pubnub.com/docs/angularjs-javascript/pubnub-javascript-sdk If that isn't sufficient you'll need to modify the 'messages' array order after updating it – Daniel Beck Jul 09 '15 at 18:32
  • 'reverse' wil reverse the item order but has no influence on where items are added to the existing list. – user1242574 Jul 09 '15 at 18:38

1 Answers1

2

You can use an orderby with ng-repeat. It looks like you have a time value in each element, you can use that to order the list.

<tr ng-repeat="item in history | orderBy:'start'">

if you have an id in the elements you can also use "track by" so if you have for instance an id field. This allows angular to show duplicates. However this doesn't seem to be your issue. from their wiki:

By default, ngRepeat does not allow duplicate items in arrays. This is because when there are duplicates, it is not possible to maintain a one-to-one mapping between collection items and DOM elements.

If you do need to repeat duplicate items, you can substitute the default tracking behavior with your own using the track by expression.

<tr ng-repeat="item in history track by $id">
Doug E Fresh
  • 820
  • 8
  • 22