私はモバイル試験アプリを作っています。質問は多肢選択式です。私は前と次の質問ボタンがあります。 $ 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 paramsに(私が調査したところによると){reload:true, inherit:false}
を追加しようとしましたが、動作しません。
これは質問に対する答えを提供しません。批評をしたり、著者の説明を求めるには、投稿の下にコメントを残してください。 - [レビューから](/レビュー/低品質の投稿/ 13945362) –
私は著者です。 – Zik