我正在我正在构建的 Angular 应用程序中创建一个简单的随机播放/取消随机播放功能。这个想法是有一个按钮,单击它会克隆数组,对数组进行洗牌,然后返回数组的洗牌顺序,或者如果数组已经被洗牌,它将返回原始数组的克隆,因此用户可以恢复到原来的顺序。
我遇到的问题是我无法弄清楚如何将原始订单克隆返回到 View 。
这是一个 fiddle :http://jsfiddle.net/nf6j1qvz/
这是一些函数代码:
$scope.shuffleThis = function(array) {
if(!$scope.isShuffled){
$scope.isShuffled = true;
$scope.unshuffled = array.slice(0);
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;
}else{
console.log('unshuffling');
$scope.isShuffled = false;
array = $scope.unshuffled;
return array;
}
}
最佳答案
您可以按如下方式更改 ng-click
<button ng-click="array = shuffleThis(array)">
你就完成了!
普朗克:
关于Javascript/Angular 在洗牌后返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28820715/