0

I am trying have my angular ng-repeat randomize the items when they appear. The random function I am currently using is below, however when i use this function I get a infdig, causing all sorts of problems. I dont want to do the randomize in the controller because both of these ng-repeats come from the same entry where there is a url and a name but the entry id is in both instances so it would be easier if I dont have to create separate arrays. So does anyone know of a random filter that can be used for this, that wont give me the infdig problems?

 $scope.random = function(){
    return 0.5 - Math.random();
  };  


<div ng-repeat="actor in actors | orderBy:random">
          <div class="col-md-2">
            <div class='imageDropTo'>
              <img class='imageDropTo' src={{actor.url}} data-id=   {{actor.id}}>
            </div>
          </div>
        </div>


 <div ng-repeat="actor in actors | orderBy:random">
            <div class='col-md-2'>
              <p id='{{actor.id}}' class='namesToDrag'> {{actor.name}} </p>
          </div>
        </div>
Billy
  • 823
  • 3
  • 12
  • 28
  • What do you hope to accomplish by sorting by number? Usually you'd order by a property – Robin-Hoodie Apr 28 '15 at 05:06
  • Sorry if i was confusing. I'm not looking to sort by number, but by name in a random order. I just dont want to sort in the controller and separate the name and the associated id. – Billy Apr 28 '15 at 05:08
  • Sorting by _name in a random order_? Could you clarify? – Robin-Hoodie Apr 28 '15 at 05:10
  • in one ng-repeat i have a url, in the other i have a name. I want to sort or shuffle these in a completely random order. So for example each time someone refreshes the page the url's and names will be in a different order. the orderBy:random using the $scope.random function currently does this, however there is a infinite loop that occurs as a result, which causes some glitches. Does this clarify? – Billy Apr 28 '15 at 05:16
  • So you're not looking to order by name, just display the name (in your second ng-repeat)? – Robin-Hoodie Apr 28 '15 at 05:18
  • @NexusDuck. He is simply trying to randomize the order of the list. – Zee Apr 28 '15 at 05:20
  • can you clarify what you mean? I'm not looking to order by name, but I am looking for the names to be in a different order each time the page is refreshed – Billy Apr 28 '15 at 05:20
  • 2
    In that case this is a duplicate question: http://stackoverflow.com/questions/21586369/random-orderby-in-angularjs-1-2-returns-infdig-errors – Robin-Hoodie Apr 28 '15 at 05:24
  • 1
    http://stackoverflow.com/a/21587316/3894168 answer should help you. – Zee Apr 28 '15 at 05:25
  • When you make a fiddle for your question, helping will be easier :) http://jsfiddle.net/zjvsu/780/ works for me. – Stefan van de Vooren Apr 28 '15 at 06:56

0 Answers0