2016-08-22 17 views
0

我想通過嵌套的ng-repeat顯示問題列表和他們的選擇,我看到很多類似的問題,但仍然無法解決我的問題。我的問題是我可以看到問題列表,但選擇沒有得到顯示,在開發人員工具中,我只能看到評論的ng-repeat線代替選擇。嵌套的ng-repeat和無線電或複選框輸入不起作用

這是我的看法

<div ng-repeat ="question in questions track by $index" class="panel panel-default" ng-show="showQuestions"> 
    <div class="panel-heading"> 
     <div class="panel-title"> 
      <a href="div#{{$index}}" class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" > 
       {{question.QuestionTxt}} 
      </a> 
      <div class="row height"></div> 
      <div class="row" ng-show="question.QuestionTypeTxt == 'RadioButton'"> 
        <div class="col-xs-3" ng-repeat ="answer in questions.QuestionAnswers track by $index"> 
         <input type="radio" ng-model="selectedChoice.choice" ng-value="{{answer.AnswerTxt}}" name="{{question.QuestionTxt}}"/>{{answer.AnswerTxt}} 
        </div> 
      </div> 
      <div class="row" ng-show="question.QuestionTypeTxt == 'Checkbox'"> 
        <div class="col-xs-3" ng-repeat ="answer in questions.QuestionAnswers track by $index"> 
         <input type="checkbox" ng-model="selectedChoice.choice" ng-value="{{answer.AnswerTxt}}" name="{{question.QuestionTxt}}"/>{{answer.AnswerTxt}} 
        </div> 
      </div> 
     </div> 
    </div> 
</div> 

這裏是我的控制器

$scope.questions = []; 
       $scope.selectedChoice = { choice:"" }; 

$scope.addQuestions = function() { 
     $scope.showQuestions = true; 
     rmat3Service.getQuestionsForSection().then(function (data) { 
       angular.forEach(data,function(a) { 
        $scope.questions.push(a); 
       }); 
     }); 
} 

這是我的JSON數據:

var questions = new List<Question>(); 
var answers = new List<QuestionAnswer>(); 
answers.Add(new QuestionAnswer() 
{ 
    AnswerTxt = "Yes", 
}); 
answers.Add(new QuestionAnswer() 
{ 
    AnswerTxt = "No" 
}); 
answers.Add(new QuestionAnswer() 
{ 
    AnswerTxt = "Yes Verified" 
}); 
answers.Add(new QuestionAnswer() 
{ 
    AnswerTxt = "Not Applicable" 
}); 
questions.Add(new Question() 
{ 
    QuestionId = 1, 
    QuestionTxt = "Are aisles clear of product on the floor?", 
        QuestionTypeTxt = "RadioButton", 
        QuestionAnswers = answers 
}); 
questions.Add(new Question() 
{ 
    QuestionId = 2, 
    QuestionTxt = "Automated Car Wash", 
    QuestionTypeTxt = "Checkbox", 
    QuestionAnswers = answers 
}); 
return Json(questions, JsonRequestBehavior.AllowGet); 
+0

你能通過'console.log($ scope.questions)'驗證你有什麼嗎? –

回答

1

的問題是,你正在試圖循環questions.QuestionAnswers這不存在。它應該是question.QuestionAnswers

<div class="col-xs-3" ng-repeat ="answer in question.QuestionAnswers track by $index"> 
+0

你說得對,謝謝。 –