【发布时间】:2017-02-19 13:23:17
【问题描述】:
我正在制作一个移动考试应用程序。题目为多项选择题。我有上一个和下一个问题按钮。它使用 $state.go 转换到同一个 HTML 页面,但有不同的问题和选择。
// this function is in a service and the service is used by the controller
this.switchTemplateHTML = function(type, category) {
if(type == "multiple") {
$state.go("test-item-multiple-choice", {categoryParam: category.category_name, questionPointer: self.questionIndexPointer});
}
}
问题存储在包含问题、选项(数组)和用户答案(选项索引)的对象数组中。要转到上一个或下一个问题,我在控制器中有以下代码:
$scope.nextQuestion = function() {
if(service.questionIndexPointer+1 < $scope.questions.length) {
service.questionIndexPointer++;
service.switchTemplateHTML($scope.questions[service.questionIndexPointer].type, $scope.category);
}
};
$scope.previousQuestion = function() {
if(service.questionIndexPointer > 0) {
service.questionIndexPointer--;
service.switchTemplateHTML($scope.questions[service.questionIndexPointer].type, $scope.category);
}
};
我在控制器中有 $scope.init 函数。每次进入下一个问题时都会调用 scope.init。它根据 $stateParams.categoryParam 加载类别、问题和选择。里面还有这段代码:
// When clicking on a radio button choice, the answer to the current question is updated
$scope.$watch('radioChoice.choice', function(newval, oldval) {
if(typeof newval != 'undefined') {
if($scope.currentQuestion != null) {
$scope.currentQuestion.usersAnswer = newval;
}
}
});
还有这个:
// Watch the function that returns service.timerInString
// Directly watching service.timerInString doesn't work
$scope.$watch(function() {
return service.timerInString;
}, function(newval, oldval) {
$scope.time = newval;
// Not relevant
if(newval === "00:00") {
service.getCategory($scope.category.category_name).categoryIsFinish = true;
var alertTimeup = $ionicPopup.alert({
template: '<center>Your time is up!</center>'
});
alertTimeup.then(function(res) {
service.stopTimer();
service.saveAnswersInCategory();
$scope.time = '00:00';
$state.go("home");
});
}
// I added this so that the radio button is SUPPOSED to be selected when going back to a question that has been answered
if(typeof $scope.currentQuestion.usersAnswer != 'undefined') {
$scope.radioChoice.choice = $scope.currentQuestion.usersAnswer;
}
});
现在问题来了:当我转到之前已经回答的问题时,它们不会显示选择了哪个(但如果您 console.log 当前问题它有答案)。但是当我转到下一个已经回答的问题时,它会显示选定的答案。我在 $scope.init() 函数中放置了一个 console.log ,每次单击下一个问题按钮时它都会在控制台中打印,但是单击上一个问题按钮时它不会打印,因此这意味着 init() 函数去的时候不叫。为什么它没有被调用,我有相同的 state.go 只是在问题索引指针上有所不同。我还尝试在 state.go 参数中添加{reload:true, inherit:false}(根据我的研究),但它也不起作用。
【问题讨论】:
标签: javascript html angularjs ionic-framework angular-ui-router