javascript - AngularJS 1.x 在 ng-repeat 中一次显示一项,单击下一步应显示下一项,依此类推

标签 javascript angularjs html angularjs-scope angularjs-ng-repeat

我正在开发一个测验 Web 应用程序,我必须在 3 个不同的选项卡(数学、GK、英语)中总共显示 100 个问题,这些问题是使用 Angular 过滤器进行过滤的。但每个选项卡都一次性显示所有问题(下一个/上一个按钮无法正常工作),我尝试使用此处提供的各种解决方案,但它们也不起作用。 应用程序的另一部分显示所有 100 个问题按钮(在问题 div 的右侧),用户可以导航到任何问题号。单击该按钮。

Angular 代码片段:

app.controller('examController',['$scope','$http', function ($scope,$http) {

$http.get('/static/exam-files/question').then(function (response) {
    $scope.questions = response.data;
    console.log($scope.questions);
    $scope.mode = "quiz";
})

$scope.qnIndex = 0;

$scope.next = function () {
    if ($scope.qnIndex >= $scope.questions.length - 1) {
        $scope.qnIndex = 0;
    } else {
        $scope.qnIndex++;
    }
};

$scope.previous = function () {
    if ($scope.qnIndex >= $scope.questions.length - 1) {
        $scope.qnIndex = 0;
    } else {
        $scope.qnIndex--;
    }
};
}]);

2 个选项卡的 HTML 代码片段-

   <uib-tab index="0" heading="Verbal">
                <div class="tab-pane fade active in qnPadding" id="Verbal"  ng-repeat="question in questions | filter:{type:'verbal'}" ng-if="qnIndex == $index">
                    <div class="q-div q-height q-background">
                        <span>Q.No: <span class="q-num">
 {{question.questionId}}</span><p>{{question.questionDesc}}</p></span>
                    </div>


                    <div class="options well opt-background" style="margin: 10 10 15 15">
                        <!-- List group -->
                        <div class="radio" ng-repeat="radiobox in question.options">
                            <label>
                                <input type="radio" name="optionsRadios">
                                {{radiobox}}
                            </label>
                        </div>

                    </div>


                    <div>
                        <div class="btn-group " role="group" aria-label="...">
                            <button type="button" class="btn btn-default btn-pre" ng-disable="$index = 0" ng-click="previous()">Previous</button>
                            <button type="button" class="btn btn-default btn-next" ng-click="next()">Next</button>
                        </div>
                        <div class="btn-group pull-right q-background" role="group" aria-label="...">
                            <button type="button" class="btn btn-default btn-save">Mark</button>
                            <button type="button" class="btn btn-default btn-unsel">Un-Select</button>
                        </div>

                    </div>

                </div>

            </uib-tab>
            <uib-tab index="1" heading="Math">

                <div class="tab-pane active in qnPadding" id="Math" ng-repeat="question in questions | filter: { type: 'math' }" ng-if="qnIndex == $index">
                    <div class="options well" >
                        <div> <img src="">

                        <span>Q.No: <span class="q-num">{{question.questionId}}</span><p>{{question.questionDesc}}</p></span></div>
                        <div class="radio" ng-repeat="x in question.options">
                            <label >
                                <input type="radio" name="optionsRadiosm">
                                {{x}}
                            </label>
                        </div>
                    </div>


                    <div>
                        <div class="btn-group" role="group" aria-label="...">
                            <button type="button" class="btn btn-default" ng-click="previous()">Previous</button>

                            <button type="button" class="btn btn-default" ng-click="next()">Next</button>
                        </div>
                        <div class="btn-group pull-right" role="group" aria-label="...">
                            <button type="button" class="btn btn-default">Mark</button>
                            <button type="button" class="btn btn-default">Un-Select</button>
                        </div>

                    </div>

                </div>


            </uib-tab>

索引明智按钮----

 <div class="col-md-3">
        <div class="panel panel-default">
            <!-- Default panel contents -->
            <div class="panel-heading opt-background"> Welcome candidate</div>
            <div class="panel-body">
                <p>your {{Test}} has {{question.count}} question, click on any question no below to navigate
                </p>
                <div ng-repeat="question in questions">
                    <button type="button" class="btn btn-info btn-circle btn-lg" ng-click="goTo($index)">{{$index+1}}</button>

                </div>
            </div>


        </div>
    </div>

感谢任何帮助。提前致谢。

最佳答案

刚刚修改了@tanmay的答案。将新记录附加到最后一条记录。

var app = angular.module('myApp', []);
app.controller("myCtrl", function($scope) {
  $scope.myArray = [123, 3432, 4645, 7657, 4575646, 234, 43, 555]
  $scope.count = 0
  $scope.clicked = function() {
    $scope.count = $scope.count + 1
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.js"></script>
<body ng-app="myApp">
  <div ng-controller="myCtrl">
    <table>
      <tr ng-repeat="obj in myArray">
        <td ng-show="$index <= count">{{obj}}</td>
        <td ng-show="$last && count !== myArray.length - 1">
          <button ng-click="clicked()">+</button>
        </td>
      </tr>
    </table>
  </div>
</body>

关于javascript - AngularJS 1.x 在 ng-repeat 中一次显示一项,单击下一步应显示下一项,依此类推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663772/

相关文章:

javascript - 无法将 Google map 标记对象保存到隐藏 html 字段中的数组

javascript - 如何将多个结果返回到数组中?

javascript - ADAL JS - 获取 token : Token Renewal Operation failed due to timeout

ruby-on-rails - Angular 的 ng-src 在 Rails 生产环境中的使用

javascript - 无法在 javascript 中的嵌套对象/数组中正确循环

java - 为什么我在 AngularJs 帖子中收到 400 个错误请求?

angularjs - IE 11 - 图像在破坏 View 后保留在浏览器内存中

javascript - 通过 onclick 启动的视频不会在 Firefox 中启动,但可以在 Chrome 中启动

html - ID 中的伪类选择器

jquery - HTML 5 CSS 页脚堆叠