Recently, during the process of doing projects, I asked to build an examination system. On the answer page, the radio box + ng-repeat is involved to display the test paper. After finishing, the answer will be passed to the background. Here we mainly talk about several key points of radio box + ng-repeat.
The foreground code is as follows:
<div class="container" width="1900px" data-ng-controller="QuestionSettingCtrl"> <div class="text-center" data-ng-repeat="item in items"> <table> <tr><td data-ng-bind="$index+1 +'、 '+" colSpan="4"></td></tr> <tr><td> </td></tr> <tr> <td><input type="radio" value="A" data-ng-model="answer[$index]" />A <span data-ng-bind="item.op1"></span> </td> <td><input type="radio" value="B" data-ng-model="answer[$index]" />B <span data-ng-bind="item.op2"></span> </td> <td><input type="radio" value="C" data-ng-model="answer[$index]" />C <span data-ng-bind="item.op3"></span> </td> <td><input type="radio" value="D" data-ng-model="answer[$index]" />D <span data-ng-bind="item.op4"></span> </td> </tr> </table> <br> <br> </div> <div class="text-center"><button class="btn btn-primary" style="margin: 0 auto;" data-ng-click="submit()">submit</button></div> <br><br><br><br> </div>
Note that in a set of radios, the ng-model used is the same, the principle is similar to name, the value of ng-model is directly bound to the array of js (using $index to determine which one is bound to).
The js code is as follows:
('QuestionSettingCtrl',function($scope, $http){ $ = new Array(30); $http({ url : "/getexamquestions", method : "post", params : { 'account' : 30 } }).success(function(res){ $=res; }); $ = function(){ $http({ url : "/submitanswer", method : "post", params : { 'answer' : $ } }).success(function(res){ alert("You did it right"+res+"question!"); }); } });
I created an answer array in the initialization of the controller, which is two-way bound to radio in html. In the submit method, I can complete the comparison of the answers by submitting it directly to the background.
The above method of implementing the angularjs single box + ng-repeat is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.