0

I've searched and only can find things related to a random item, but what I want to do is get (for example) 7 random items from a larger group of items.

So right now I have code which gets me the first 7 items in my data, but I want to pull 6 random items instead.

<ul class="block-grid-4">
    <li ng-repeat="icon in icons | limitTo:7"><i class="fa fa-fw fa-{{icon.id}}"></i> fa-{{icon.id}}</li>
  </ul>

How can I simply get 7 (or any arbitrary number) items?

My service is

.factory('FaIconFactory', ['$http',
    function($http) {

        return {
            getIcons: function() {
                var icons = $http.get('scripts/data/fa_icons.js').then(function(response) {
                    return response.data.icons;
                });

                return icons;
            }
        };
    }
]);
Steve
  • 14,401
  • 35
  • 125
  • 230
  • 1
    When you call the service method and store the icons in the scope variable, shuffle the array. Or, create a filter that does that, so you aren't messing with the raw data (and add the `limitTo:7` after that filter). If you want to randomize **how many** are displayed, generate a random number when you get the icons, and set it in the scope. Use that scope variable instead of `7` – Ian Jun 27 '14 at 14:26
  • Here's an example of what I meant: http://jsfiddle.net/4RAXk/ - unfortunately, i couldn't get a `filter` to work because it was causing too many digests in Angular – Ian Jun 27 '14 at 14:53

0 Answers0