Angular state.go不刷新页面因此没有显示所选的单选按钮

时间:2016-10-11 02:57:19

标签: javascript html angularjs ionic-framework angular-ui-router

我正在制作移动考试应用。问题是多种选择。我有上一个和下一个问题按钮。它使用$ 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 params中添加{reload:true, inherit:false}(根据我研究的内容),但它也不起作用。

1 个答案:

答案 0 :(得分:0)

preg_replace添加到配置中。