Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – ng-repeat angular中的shuffle数组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个测验,每次我开始测验时我想要改变问题,这样他们每次都不会出现在同一个顺序中.

我在我的HTML代码中有这个:

<div ng-repeat="question in questions | filter:ids | orderBy:randomSort">
    <div id="question">{{question.question}}</div><img id="nextImg" ng-src="../app/img/next.png" ng-click="next()" />
    <img class="quizImg" ng-hide="{{question.imagE}}==null" ng-src="{{question.imagE}}" />

    <div class="answer" ng-repeat="answer in question.answers">
        <input type="radio" ng-click="checked(answer)">{{answer.answer}}
    </div>
    <!--input id="nextQuestion" type="button" ng-click="next()" value="{{Buttontext}}"-->
</div>

这在我的控制器中

lycheeControllers.controller('quizCtrl',['$scope','$http',function ($scope,$http) {
    $http.get('json/questions.json').success(function (data) {
        //all questions
        $scope.questions = data;

        $scope.titel = "check your kNowledge about lychees"


        $scope.randomSort = function(question) {
                      return Math.random();
                    };

        //filter for getTing answers / question
        $scope.ids = function (question) {
            return question.id == number;
        }

        $scope.find = function (id) {
            if (id == number) {
                return true;
            }
        }


        $scope.next = function () {
            if (!(number == (data.length))) {
                //questionId++;
                number++;
                if (correct == truE) {
                    points++;
                }
                //alert(points);
            } else {
                if (!end) {
                    if (correct == truE) {
                        points++;
                        end = true;
                    }
                }

                alert("Quiz finished: your @R_756_10586@l score is: " + points);
            }
            correct = false;
        }

        $scope.checked = function (answer) {
            //alert(answer.answer);

            if (answer.correct == "yes") {
                correct = true;
            } else {
                correct = false;
            }

            //alert(correct);
        }


    });

}])
;

不幸的是,这根本不起作用.希望有人可以帮助我这个吗?

解决方法

Thx到 http://bost.ocks.org/mike/shuffle/ @H_502_27@使用这个改组功能

与它相关的是,输入数组保持可绑定,因为混洗不会创建新数组,而是在同一引用上进行混洗.

// -> Fisher–Yates shuffle algorithm
var shuffleArray = function(array) {
  var m = array.length,t,i;

  // While there remain elements to shuffle
  while (m) {
    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = arraY[R_947_11845@];
    arraY[R_947_11845@] = arraY[i];
    arraY[i] = t;
  }

  return array;
}

旁注:lodash的_.shuffle(数组)也不起作用,因为它们正在创建一个破坏绑定的新数组(因此一个混洗数组不会触发模型变脏)

要完成工作解决方案的答案,请执行以下步骤:

>复制该功能,以便您可以在控制器内使用它.@H_502_27@>在$http结果回调中调用它:

    

$http.get('json/questions.json').success(function (data) {
  //all questions
  $scope.questions = data;

  shuffleArray($scope.questions);

  ...
}

大佬总结

以上是大佬教程为你收集整理的angularjs – ng-repeat angular中的shuffle数组全部内容,希望文章能够帮你解决angularjs – ng-repeat angular中的shuffle数组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。