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"
2
  • please provide the data in chats Commented Feb 22, 2016 at 4:49
  • I just did check it out. Commented Feb 22, 2016 at 5:00

2 Answers 2

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...

Sign up to request clarification or add additional context in comments.

1 Comment

This is the working solution. Marking this as the right answer.
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'}];
});

3 Comments

No it doesn't, and yes I already tried the solution you referenced.
Why dont you create your own plunkr so that we can get more idea about issue you are having!
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.