1

私はアンケートをまとめています。各質問には3つの選択肢があります。質問を含む各オブジェクトには、 'dependsOn'という別のオブジェクトがあります。 質問1で「はい」を選択すると、次の2つの質問が表示され、そうでない場合はスキップされます。 私はこの小さなアプリのための助け、アイデア、提案を感謝します。 my codepen別の選択に応じてJSONオブジェクトを使用する角型単一ページアプリケーション

<div class="main" ng-app="MyApp"> 

<div ng-controller="AppCtrl as q"> 

<h1>{{ q.title }}</h1> 

<form name="collection" novalidate> 

    <div class="questionnaire"> 

     <div class="questions"> 

      <div class="question" ng-repeat="question in q.questions" ng-show="question.hrsQuestionOrderNumber === q.currentQuestion"> 
       <h2>Question {{ question.hrsQuestionOrderNumber }} <span>of {{ q.questions.length }}</span></h2> 
       <p> 
        {{ question.descriptionLong }} 
       </p> 

       <ng-form name="subForm{{question.questionID}}" class="options"> 
        <md-radio-group ng-model="question.hrsAnswerId" ng-change="q.watchForm(subForm{{ q.currentQuestion }})" required> 
         <md-radio-button ng-repeat="option in question.choiceModels" ng-value="option.description" required> 
         {{ option.description }} 
         </md-radio-button> 
        </md-radio-group> 
       </ng-form> 
      </div> 

     </div> 

     <nav class="clearfix"> 
      <md-button class="md-primary md-raised" ng-click="q.questionChange('prev')" ng-disabled="q.prev">Previous</md-button> 
      <md-button class="md-primary md-raised" ng-click="q.questionChange('next')" ng-disabled="q.next">Next</md-button> 
     </nav> 
    </div> 

</form> 

角度コード::ここに は、私は一緒に入れCodePenある

angular 
.module('MyApp',['ngMaterial', 'ngMessages']) 
.controller('AppCtrl', function($timeout, $scope) { 

const context = this; 

context.title = 'Questionnaire with Questions Depending on Choices'; 
context.currentQuestion = 1; 
context.next = true; 
context.prev = true; 
context.nextButton = true; 

context.form = $scope.collection; 

function disableButton() { 

    if (context.currentQuestion === 1) { 
     context.prev = true; 
     context.next = false; 
    } else { 
     context.prev = false; 
     context.next = false; 
    } 
}; 

context.questionChange = function (go) { 

    if (go === 'prev') { 
     context.currentQuestion = context.currentQuestion - 1; 
    } 

    if (go === 'next') { 

     context.currentQuestion = context.currentQuestion + 1; 

     $timeout(function() { 
      context.next = true; 
     }); 
    } 

    disableButton(); 
}; 

context.watchForm = function (currentForm) { 

    if (currentForm.$invalid === false) { 

    if (context.currentQuestion !== context.questions.length) { 
     context.next = false; 
    } 
    } else { 
    console.log('form NOT in scope'); 
    } 

}; 

context.questions = [ 
      { 
       "questionID": 1, 
       "hrsQuestionOrderNumber": 1, 
       "descriptionLong": "Do you collect money from anyone (students, employees, or other sources)?", 
       "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1 
       }, 
       { 
        "description": "No", 
        "answerId": 1 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 3 
       } 
       ], 
       "dependsOn": null 
      }, 
      { 
       "questionID": 2, 
       "hrsQuestionOrderNumber": 2, 
       "descriptionLong": "Are pre-numbered receipts given to the person paying money? If individual receipts are not given, do you use an approved PBO/S&I collection document?", 
       "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1 
       }, 
       { 
        "description": "No", 
        "answerId": 1 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 3 
       } 
       ], 
       "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 1, 
       "hrsAnswerId": 1 
       } 
      }, 
      { 
       "questionID": 3, 
       "hrsQuestionOrderNumber": 3, 
       "descriptionLong": "Do cash receipts or logs contain sufficient detail to accurately describe the nature of the transaction?", 
       "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1 
       }, 
       { 
        "description": "No", 
        "answerId": 1 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 3 
       } 
       ], 
       "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 1, 
       "hrsAnswerId": 1 
       } 
      }, 
      { 
       "questionID": 4, 
       "hrsQuestionOrderNumber": 4, 
       "descriptionLong": "Do receipts or logs identify individuals and not groups of individuals (such as a class)?", 
       "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1 
       }, 
       { 
        "description": "No", 
        "answerId": 1 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 3 
       } 
       ], 
       "dependsOn": null 
      }, 
      { 
       "questionID": 5, 
       "hrsQuestionOrderNumber": 5, 
       "descriptionLong": "For money collected, is it always deposited and never used for purchases?", 
       "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1 
       }, 
       { 
        "description": "No", 
        "answerId": 1 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 3 
       } 
       ], 
       "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 4, 
       "hrsAnswerId": 1 
       } 
      }, 
      { 
       "questionID": 6, 
       "hrsQuestionOrderNumber": 6, 
       "descriptionLong": "For money not yet deposited, is it kept in a secure location?", 
       "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1 
       }, 
       { 
        "description": "No", 
        "answerId": 1 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 3 
       } 
       ], 
       "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 4, 
       "hrsAnswerId": 1 
       } 
      }, 
      { 
       "questionID": 7, 
       "hrsQuestionOrderNumber": 7, 
       "descriptionLong": "Do you keep a file of original deposit documentation—including cash receipts or logs—together?", 
       "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1 
       }, 
       { 
        "description": "No", 
        "answerId": 1 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 3 
       } 
       ], 
       "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 4, 
       "hrsAnswerId": 1 
       } 
      } 
      ]; 

}) 
.config(function($mdIconProvider) { 
}); 

答えて

0

解決策は質問をループし、選択した回答に応じて質問を評価することでした。これにより、選択された回答に基づいていくつかの質問をスキップすることができました。 ここに私のコードペンはあります:My CodePend

angular 
.module('MyApp', ['ngMaterial', 'ngMessages']) 
.controller('AppCtrl', function ($timeout, $scope, $filter) { 

    const context = this; 

    context.title = 'Questions Depending on Choices'; 
    context.heading = 'Questionnaire'; 
    context.currentQuestion = 1; 
    context.next = true; 
    context.prev = true; 
    context.nextButton = true; 

    function disableButton() { 

     if (context.currentQuestion === 1) { 
      context.prev = true; 
      context.next = false; 
     } else { 
      context.prev = false; 
      context.next = false; 
     } 
    }; 

    context.questionChange = function(go) { 

     if (go === 'prev') { 

      context.currentQuestion = context.currentQuestion - 1; 
      //Go to the prev (in order) that has an selectedAnswer 
      var prevQuestions = $filter("filter")(context.questions, { "hrsQuestionOrderNumber": context.currentQuestion }); 
      if (!prevQuestions[0].hrsAnswerId) { 
       context.questionChange(go); 
      } 
     } 

     if (go === 'next') { 
      //in order - check the dependsOn for Q&a required 

      context.currentQuestion = context.currentQuestion + 1; 
      var nextQuestions = $filter("filter")(context.questions, { "hrsQuestionOrderNumber": context.currentQuestion }); 
      if (nextQuestions.length > 0) { 
       var dependsOn = nextQuestions[0].dependsOn; 
       if (dependsOn) { 
        var dependentQuestions = $filter("filter")(context.questions, { "questionID": dependsOn.hrsQuestionLink }); 
        if (dependentQuestions[0].hrsAnswerId !== dependsOn.hrsAnswerId) { 
         context.questionChange(go); 
        } 
       } 
      } 

      $timeout(function() { 
       context.next = true; 
      }); 
     } 

     disableButton(); 
    }; 

    context.watchForm = function(currentForm) { 

     if (currentForm.$invalid === false) { 

      if (context.currentQuestion !== context.questions.length) { 
       context.next = false; 
      } 
     } else { 
      console.log('form NOT in scope'); 
     } 

    }; 



    context.questions = [ 
     { 
      "questionID": 1533, 
      "hrsQuestionOrderNumber": 1, 
      "descriptionLong": "Do you collect money from anyone (students, employees, or other sources)?", 
      "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1000 
       }, 
       { 
        "description": "No", 
        "answerId": 1001 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 1148 
       } 
      ], 
      "dependsOn": null 
     }, 
     { 
      "questionID": 1534, 
      "hrsQuestionOrderNumber": 2, 
      "descriptionLong": "Are pre-numbered receipts given to the person paying money? If individual receipts are not given, do you use an approved PBO/S&I collection document?", 
      "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1000 
       }, 
       { 
        "description": "No", 
        "answerId": 1001 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 1148 
       } 
      ], 
      "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 1533, 
       "hrsAnswerId": 1000 
      } 
     }, 
     { 
      "questionID": 1535, 
      "hrsQuestionOrderNumber": 3, 
      "descriptionLong": "Do cash receipts or logs contain sufficient detail to accurately describe the nature of the transaction?", 
      "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1000 
       }, 
       { 
        "description": "No", 
        "answerId": 1001 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 1148 
       } 
      ], 
      "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 1533, 
       "hrsAnswerId": 1000 
      } 
     }, 
     { 
      "questionID": 1536, 
      "hrsQuestionOrderNumber": 4, 
      "descriptionLong": "Do receipts or logs identify individuals and not groups of individuals (such as a class)?", 
      "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1000 
       }, 
       { 
        "description": "No", 
        "answerId": 1001 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 1148 
       } 
      ], 
      "dependsOn": null 
     }, 
     { 
      "questionID": 1537, 
      "hrsQuestionOrderNumber": 5, 
      "descriptionLong": "For money collected, is it always deposited and never used for purchases?", 
      "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1000 
       }, 
       { 
        "description": "No", 
        "answerId": 1001 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 1148 
       } 
      ], 
      "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 1536, 
       "hrsAnswerId": 1000 
      } 
     }, 
     { 
      "questionID": 1538, 
      "hrsQuestionOrderNumber": 6, 
      "descriptionLong": "For money not yet deposited, is it kept in a secure location?", 
      "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1000 
       }, 
       { 
        "description": "No", 
        "answerId": 1001 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 1148 
       } 
      ], 
      "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 1536, 
       "hrsAnswerId": 1000 
      } 
     }, 
     { 
      "questionID": 1539, 
      "hrsQuestionOrderNumber": 7, 
      "descriptionLong": "Do you keep a file of original deposit documentation—including cash receipts or logs—together?", 
      "choiceModels": [ 
       { 
        "description": "Yes", 
        "answerId": 1000 
       }, 
       { 
        "description": "No", 
        "answerId": 1001 
       }, 
       { 
        "description": "None/Not applicable", 
        "answerId": 1148 
       } 
      ], 
      "dependsOn": { 
       "hrsQuestionId": 0, 
       "hrsQuestionLink": 1536, 
       "hrsAnswerId": 1000 
      } 
     } 
    ]; 

}) 
.config(function($mdIconProvider) { 
}); 
1

を私はあなたの問題が何であるかを理解していませんか?

  1. キーとして質問の数を含み、値

    としてインデックスに応答するオブジェクトを作成{ '1'、1 '2':0、...}

  2. 後すべての答えは、あなたが何を選択したかに依存して、一連の質問を繰り返し、次の質問を選択するだけです。
+0

データはcontext.questionsにあります。各質問はDEPENDSONオブジェクトを持っており、時にはそれがnullであり、他の場合には、それは "DEPENDSON" を示す:{ "hrsQuestionId":0、 "hrsQuestionLink":1、 "hrsAnswerId":1 } これは、このことを意味し質問は質問1のhrsAnswer 1を選択したユーザーに依存します。私はこれの初心者です。 – iChido

+0

歩行サイクルでは、dependsOn === nullかどうかを確認することができます。 dependsOn!== nullの場合、答えの配列で答えをチェックするだけです。 –

関連する問題