在角度ng重复中迭代对象的数组

Iterating array of object in angular ng-repeat

本文关键字:迭代 对象 数组 ng      更新时间:2023-09-26

我有一个这样的对象

var questions= [{ QuestionId:"1",
                  QuestionName:"loreum ispum?",
                  Answers: [{option:"A",Score:10},
                            {option:"B",Score:10}                       
                            ]
                 },
                 { QuestionId:"2",
                   QuestionName:"loreum ispum?",
                    Answers: [{option:"A",Score:10},
                              {option:"B",Score:10}                       
                             ]
                 }]

遍历 ng 重复

 <ul ng-repeat="question in vm.questions">
   <div class="">{{question.QuestionName}}</div>
   <input type="text" ng-model="getdetails.QuestionId" ng-value="question.QuestionId" style="display:none"/>
   <li ng-repeat="answerswer in question.Answers track by $index">
      <input type="radio" name="questionId_[$index]">" ng-model="getdetails.Score" ng-value="answerswer.Score" />
   </li>
   <button class="btn btn-success no-broder-radius" ng-click="change(getdetails)">Submit</button>
 </ul>

并单击提交按钮,我需要获取问题 ID 和选定的输入值。

控制器:

     $scope.getdetails={};
    $scope.change=function(getDetails) {
     // I am getting only input radio value not getting question id
} 

我只得到输入无线电值而没有得到问题ID

如果getDetails是一个$scope变量,那么你可以直接在控制器中访问它,你不需要从视图中传递它。

试试这个:

 <ul ng-repeat="question in vm.questions">
   <div class="">{{question.QuestionName}}</div>
   <input type="text" ng-model="getDetails.QuestionId" ng-value="question.QuestionId" style="display:none"></input>
   <li ng-repeat="answerswer in question.Answers track by $index">
     <input type="radio" name="questionId_[$index]">" ng-model="getdetails.Score" ng-value="answerswer.Score" />
   </li>
   <button class="btn btn-success no-broder-radius" ng-click="change(question)">Submit</button>
</ul>

现在在更改功能中,您将可以访问完整的对象。