I tried this fiddle from this, this, this SO, and this.
I still can't make shuffle
or random
work on my end. My code has stopped here:
signupApp.filter('shuffle', function() {
var shuffledArr = [],
shuffledLength = 0;
return function(arr) {
if (!arr || !arr.length) { return; }
var o = arr.slice(0, arr.length);
if (shuffledLength == arr.length) return shuffledArr;
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
shuffledArr = o;
shuffledLength = o.length;
return arr.slice(o);
};
});
my ng-repeat
<ul ng-repeat="codes in response | shuffle">
<li ng-if="((codes.branch == formData.branches.alias) && (codes.taken == 0))">
<strong>{{codes.id}}</strong>
{{codes.code}}
</li>
</ul>
Results still do not randomize my outputs. Can someone help?
[UPDATE]: My code works in randomizing ng-repeat:
<span ng-repeat="codes in response">
<strong ng-if="((codes.branch == formData.branches.alias) && (codes.taken == 0))">
{{codes.code}}
</strong>
</span>
When limitTo:1
is added, it no longer displays anything. In my controller, the fisher-yates is run like this:
var shuffleArray = function(array) {
var m = array.length, t, i;
// While there remain elements to shuffle
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
http.get("server/fetch.php").success(function(response){
scope.response = response;
shuffleArray(scope.response);
}).error(function() {
scope.response = "error in fetching data";
});
I can't figure out what's wrong with this. When limitTo:
is set to 6, it outputs a single random value from the list. I also tried to set it to 10, and it outputs two random values from the list. I'm confused. I just want to limit it to 1 and randomize it.