我正在创建一个测验,每当我开始测验时,我想要改变问题,这样每次都不会出现相同的顺序。
我的html代码中有这个:
<div ng-repeat="question in questions | filter:ids | orderBy:randomSort">
<div id="question">{{question.question}}</div><img id="nextImg" ng-src="../app/img/next.png" ng-click="next()" />
<img class="quizImg" ng-hide="{{question.image}}==null" ng-src="{{question.image}}" />
<div class="answer" ng-repeat="answer in question.answers">
<input type="radio" ng-click="checked(answer)">{{answer.answer}}
</div>
<!--input id="nextQuestion" type="button" ng-click="next()" value="{{buttonText}}"-->
</div>
这在我的控制器中
lycheeControllers.controller('quizCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('json/questions.json').success(function (data) {
//all questions
$scope.questions = data;
$scope.titel = "Check your knowledge about lychees"
$scope.randomSort = function(question) {
return Math.random();
};
//filter for getting answers / question
$scope.ids = function (question) {
return question.id == number;
}
$scope.find = function (id) {
if (id == number) {
return true;
}
}
$scope.next = function () {
if (!(number == (data.length))) {
//questionId++;
number++;
if (correct == true) {
points++;
}
//alert(points);
} else {
if (!end) {
if (correct == true) {
points++;
end = true;
}
}
alert("Quiz finished: your total score is: " + points);
}
correct = false;
}
$scope.checked = function (answer) {
//alert(answer.answer);
if (answer.correct == "yes") {
correct = true;
} else {
correct = false;
}
//alert(correct);
}
});
}])
;
不幸的是,这根本不起作用。希望有人可以帮我这个吗?
答案 0 :(得分:21)
Thx to http://bost.ocks.org/mike/shuffle/ 使用这个改组函数:
它的特殊之处在于输入数组保持可绑定,因为改组不会创建一个新数组,而是在同一个引用上进行洗牌。
// -> Fisher–Yates shuffle algorithm
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;
}
旁注: lodash的_.shuffle(array)
不起作用,因为他们正在创建一个破坏绑定的新数组(因此一个混洗数组不会触发模型变脏)
要完成工作解决方案的答案,请执行以下步骤:
$http
结果回调中调用它:$http.get('json/questions.json').success(function (data) {
//all questions
$scope.questions = data;
shuffleArray($scope.questions);
...
}
答案 1 :(得分:1)
_.shuffle(collection)
答案 2 :(得分:0)
我使用 lodash _.shuffle()
随机显示主页中的产品。使用起来非常简单,如下所示:
在 home.component.ts 文件中,
products: IProduct[] = [];
constructor(private productService: ProductService) {}
ngOnInit(): void {
this.productService
.getProducts()
.subscribe((prodData: IProductsResponse) => {
this.products = _.shuffle(prodData.products);
console.log(this.products);
});
}
在 home.component.html 文件中,
<div class="col-md-4 col-sm-6" *ngFor="let product of products let idx = index">
<div class="card my-2" *ngIf="idx <= 8">
<img
[src]="product.image"
alt="{{ product.title }}"
width="200px"
height="300px"
class="card-img-top"
style="cursor: pointer"
(click)="selectProduct(product._id)"
/>
<div class="card-header">
<div class="card-title">
<p>{{ product.title.substr(0, 50) | uppercase }}</p>
</div>
</div>
</div>
</div>
希望对其他开发者有所帮助!