0

我正在制作移动考试应用程序。这些问题是多种选择类型。我有上一个和下一个问题按钮。它使用$ state.go转换到相同的HTML页面,但有不同的问题和选择。Angular state.go不刷新页面,因此不显示选定的单选按钮

// 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 params中添加{reload:true, inherit:false}(根据我所研究的内容),但它也不起作用。

回答

0

$ionicConfigProvider.views.maxCache(0);添加到配置中。

+1

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 - [来自评论](/ review/low-quality-posts/13945362) –

+0

我是作者。 – Zik