角1:重复infdig抛出错误(Angular 1: ng-repeat throws infdig

2019-09-27 03:13发布

控制器:

(function(angular) {

    var app = angular.module('t2w');

    app.factory('httpq', function($http, $q) {
        return {
            get: function() {
                var deferred = $q.defer();
                $http.get.apply(null, arguments).success(deferred.resolve).error(deferred.resolve);
                return deferred.promise;
            }
        }
    });

    app.controller('JobsCtrl', ['$scope','httpq','baseUrl', function($scope, httpq, baseUrl) {

        httpq.get(baseUrl + '/jobs/json').then(function(data) {
            $scope.jobs = data;
        }).catch(function(data, status) {
            console.error('Error', response.status, response.data);
        }).finally(function() {
        });

        $scope.random = function() {
            return 0.5 - Math.random();
        };
    }]);

})(window.angular);

视图:

...

<tbody>
    <tr ng-repeat="job in jobs | orderBy:random">
        <td class="jobtitle">
            <a href="#jobs/{{job._id}}">
                {{job.title}} m/w
            </a>
            <p>
                {{job.introText | limitTo: 150}}...
            </p>
        </td>
        <td>
            {{job.area}}
        </td>
    </tr>
</tbody>

...

JSON响应:

{
    "_id": "5880ae65ff62b610h4de2740",
    "updatedAt": "2017-01-19T12:17:37.027Z",
    "createdAt": "2017-01-19T12:17:37.027Z",
    "title": "Job Title",
    "area": "City",
    "introText": "Lorem Ipsum Sit Dolor",
    ...
}

错误:

angular.js:13920错误:[$ rootScope:infdig]

有人可以给我一个提示,为什么我得到这个错误? 已经检查的文件,我不叫我的NG-重复的功能,也不会产生每次调用一个新的阵列。

Answer 1:

如果你想ng-repeat数据随机排序,你将不得不申请自己的过滤器是这样的:

.filter('shuffle', function() {
    return function(ary) {
        return _.shuffle(ary);
    }
});

接下来用它鉴于这样的:

<tr ng-repeat='job in job | shuffle'>


Answer 2:

我做了什么来解决这个问题是在阵列中的观点随机的控制器,而不是进行排序:

function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;
    while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }
    return array;
}

$scope.jobs = shuffle($scope.jobs);


文章来源: Angular 1: ng-repeat throws infdig error