Angular 1:ng-repeat 抛出 infdig 错误 [英] Angular 1: ng-repeat throws infdig error

查看:34
本文介绍了Angular 1:ng-repeat 抛出 infdig 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器:

(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]

angular.js:13920 Error: [$rootScope:infdig]

谁能给我一个提示,为什么我会收到这个错误?已经检查了文档,我没有在我的 ng-repeat 中调用函数,也没有在每次调用时生成一个新的数组.

Can someone give me a hint why I get this error? Already checked the documentation and I do not call a function in my ng-repeat nor generate a new Array with each call.

推荐答案

我为解决该问题所做的是在控制器中而不是在视图中随机对 Array 进行排序:

What I did to resolve the problem is to sort the Array randomly in the controller and not in the view:

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 抛出 infdig 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆