0

I want to collect past chats with a limit, but this prints out all the chats.

 <div ng-repeat="(key, value) in chats|limitTo:10">
     <a href="" ng-click="ind(key, value);$event.preventDefault()" class="list-group-item">
         <span class="badge">{{value.time}}</span>
         <i class="fa fa-fw fa-calendar"></i> {{value.partner}}
     </a>
</div>

This is the code in the controller.

    $scope.chats = null;
    var chats = {}
    var id = $store.get('doctor_id');
    var d = new Date();

    doctorFactory.getChatRooms(id).then(function(x){
        for (var i in x){
            chats[i] = { partner: x[i] }
        }
        $scope.chats = dateService.doEquation(chats, d);
        console.log($scope.chats);
    }).catch(function(err){
        console.log(err);
    });

console.log($scope.chats) prints out a series of objects:

2016021085524: Object
    clock: "10:15"
    date: "02/10/2016"
    partner: "Magruder_Douglas"
    time: "0 years ago"
2016021085622: Object
    clock: "10:21"
    date: "02/10/2016"
    partner: "Magruder_Douglas"
    time: "0 years ago"
Les Paul
  • 1,260
  • 5
  • 22
  • 46

2 Answers2

2

limit does not work with objects, for that you will need to implement your own custom filter :

app.filter('objLimitTo', [function(){
    return function(obj, limit){
        var keys = Object.keys(obj);
        if(keys.length < 1) return [];

        var ret = new Object();
        var count = 0;
        angular.forEach(keys, function(key, arrayIndex){
            if(count >= limit) return false;
            ret[key] = obj[key];
            count++;
        });
        return ret;
    };
}]);

i had taken this solution from SO only.. though not able to find the original answer link right now...

harishr
  • 17,807
  • 9
  • 78
  • 125
0

This solution does work but. REFERENCE

angular.module('app', []).controller('homeCtrl', function($scope) {
    $scope.limit = 3;
    $scope.expand = function(limit) { 
      $scope.limit += limit;
    }
    $scope.chat = [{ 'data' : 'one'},{ 'data' : 'two'},{ 'data' : 'three'},{ 'data' : 'four'},{ 'data' : 'five'},{ 'data' : 'six'},{ 'data' : 'seven'},{ 'data' : 'eight'},{ 'data' : 'nine'},{ 'data' : 'ten'},{ 'data' : 'eleven'},{ 'data' : 'twelve'},{ 'data' : 'thirteen'},{ 'data' : 'fourteen'},{ 'data' : 'fifteen'}];
});
Community
  • 1
  • 1
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
  • No it doesn't, and yes I already tried the solution you referenced. – Les Paul Feb 22 '16 at 05:03
  • Why dont you create your own plunkr so that we can get more idea about issue you are having! – Nitin Varpe Feb 22 '16 at 05:04
  • Well, the problem is when I get the collection from a Redis call. The above solution works like he has it (I tried by copy-and-pasting his object array) but it doesn't work with mine. – Les Paul Feb 22 '16 at 05:09