2017-01-07 1 views
0

json을 읽으려면 3 개의 중첩 된 반복을 사용하고 몇 가지 질문과 답변을 표시하십시오. 지금까지는 작동하지만 지금은 선택 응답을 저장하고 API로 보내려고합니다. 선택한 답변이 저장되지 않은 이유를 모르겠습니다. AngularJS : 양식에서 값을 얻으십시오.

내이다 :

<form> 
    <div class="panel panel-default" ng-repeat="section in questionsTest"> 
     <div class="panel-heading"> 
      <h1>{{ section.name }}</h1> 
      <h3 class="panel-title">{{ section.name }}. {{ 
       section.description }}</h3> 
     </div> 
     <div class="panel-body" ng-repeat="question in section.questions"> 
      {{ question.statement }} 
      <div ng-repeat="answer in question.answers"> 
       <!-- <label class="radio-inline"> <input type="{{ answer.type }}" ng-model="question.value" 
        name="{{ answer.id }}" id="{{ answer.id }}" value="{{ answer.id }}"> 
        {{ answer.valor }} 
       </label>--> 
       <label class="radio-inline"> <input type="{{ answer.type }}" ng-model="question.valor" 
        name="{{ question.id }}" id="{{ answer.id }}" value="{{ answer.id }}"> 
        {{ answer.valor }} 
       </label> 
      </div> 
     </div> 
    </div> 
</form> 

그리고 이것은 컨트롤러 :

$scope.question = {}; 
$scope.testing = function(){ 
    console.log($scope.question); 
}; 

$의 scope.testing 콘솔에 $ scope.question

의 가치를 볼 수있는 테스트 기능입니다
+0

당신은 plunker을 넣을 수 있습니다? –

+0

예 @ Maximus 여기 있습니다 : https://plnkr.co/edit/m35iYTFdh3G5v3IRghRI?p=preview – proktovief

+0

[내 대답] (http://stackoverflow.com/a/41525688/2545680)을 참조하십시오. –

답변

1

HTML 설정이 정확합니다. 선택한 값을 올바르게 읽지 못했습니다. 첫 번째 질문에 대한 선택한 답을 얻으려면, 다음을 사용 : 당신이 ng-modelquestion.valor 퍼팅 할 때 때문에

$scope.json.section[0].questions[0].value 

그것은 - 그것은 실제로 n-th 섹션 안에 n-th 질문입니다. 그 n 색인은 원본 데이터 구조 $scope.json 안에있는 항목의 수에 의해 정의됩니다.

모든 값을 얻으려면, 당신은 당신의 원본 객체를 반복 할 수 있습니다

$scope.testing = function() { 
    var answers = {sections: []}; 

    for (var i = 0; i < $scope.json.section.length; i++) { 
     if (!answers.sections[i]) { 
      answers.sections[i] = {answers: []}; 
     } 

     for (var j = 0; j < $scope.json.section[i].questions.length; j++) { 
      if (!answers.sections[i].answers) { 
       answers.sections[i].answers = []; 
      } 

      answers.sections[i].answers[j] = $scope.json.section[i].questions[j].value 
     } 
    } 
} 
+0

예,하지만 어떻게 할 수 있습니까? 모든 값을 얻으시겠습니까? 나는 아직도 그것을 얻지 않는다 – proktovief

+0

그냥 반복한다. 내 업데이트 된 답변을 참조 –

+0

@proktovief, 내 대답이 도움이 되었습니까? –

관련 문제