0

I have seen enough examples on how to display the limited results using limitTo. But unfortunately it isn't working as expected. Currently, it is displaying the entire list but I want to limit it to only 8 key,value items in 4/4 blocks.

Here is the plnkr.

  <ul>
      <li ng-repeat="(key,value) in stocks |limitTo :8">
            {{key +" "+ value}}
      </li>
  </ul>

 <script>
    var app = angular.module('smallcaseApp', []);
    app.controller('stockCtrl', function($scope, $http) {
    $http.get("data.json").then(function(response) {
        $scope.stocks = response.data.price;
      });
    });
 </script> 
3
  • Add a plunker that works. Yours is not working. Commented Jul 4, 2017 at 15:24
  • It is updated. Check it please. Commented Jul 4, 2017 at 15:30
  • 1
    As per the docs over here: docs.angularjs.org/api/ng/filter/limitTo, the input type can be Array/array-like, string or number. It does not work with the js object. Commented Jul 4, 2017 at 15:33

2 Answers 2

1

For limitTo input should be either array or string/number. You need to create custom filter to handle it. Here's working plunk for your issue

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

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

http://plnkr.co/edit/8d3gUqg4vHeGQWUXYrat?p=preview

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

Comments

0

First of all your plunkr is not working, angular js link should be above any other js (your app js).

You are iterating in a Object, for using limitTo here there's already a working good example, please take a look SO Link

Comments

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.