在AngularJS 1.2返回'infdig“错误随机排序依据 [英] random orderBy in AngularJS 1.2 returns 'infdig' errors

查看:141
本文介绍了在AngularJS 1.2返回'infdig“错误随机排序依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用随机排序依据排序技术作品这个问题罚款AngularJS 1.1。

Using the random orderBy sort technique in this question works fine in AngularJS 1.1.

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
    $scope.random = function() {
        return 0.5 - Math.random();
    }
}

在1.2,虽然,它把 infdig 错误到控制台,并需要更长的时间来返回排序的结果:的 http://jsfiddle.net/mblase75/jVs27/

In 1.2, though, it puts infdig errors into the console and takes a much longer time to return the sorted results: http://jsfiddle.net/mblase75/jVs27/

在控制台中的错误是这样的:

The error in the console looks like:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: $watchCollectionWatch; newVal: 42; oldVal: 36"],["fn: $watchCollectionWatch; newVal: 47; oldVal: 42"],["fn: $watchCollectionWatch; newVal: 54; oldVal: 47"],["fn: $watchCollectionWatch; newVal: 61; oldVal: 54"],["fn: $watchCollectionWatch; newVal: 68; oldVal: 61"]]

文档ORDERBY 的不具有使用功能前pressions,只有字符串前pressions的一个例子。做了一些改变,或者这是一个错误?

The documentation for orderBy doesn't have an example of using function expressions, only string expressions. Did something change, or is this a bug?

推荐答案

我不知道约previous版本,但在目前的版本中,任何Ex pression看了一个范围,如通过到 NG-重复通常每个评估消化至少两次。摘要循环,只有当所有评估前pressions的结果,在整个角应用的所有范围,是连续两次评估之间是相同的结束。

I'm not sure about previous versions, but in the current version, any expression watched on a scope, such as that passed to ng-repeat is usually evaluated at least twice per digest. The digest cycle only finishes when the results of all evaluated expressions, across all scopes of the entire Angular app, are identical between two successive evaluations.

由于各评价

<li ng-repeat="i in list | orderBy:random">{{i}}</li>

的结果调用随机()等不同的顺序,那么角将继续评估前pressions,直到达到其极限10反复消化,并引发错误。

results in calls to random(), and so a different order, then Angular will keep on evaluating the expressions, until it hits its limit of 10 digest iterations, and throws an error.

此的解决方案是设置顺序的模板外,在该控制器:

The solution to this is to set the order outside of the template, in the controller:

$scope.list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
$scope.rankedList = [];
angular.forEach($scope.list, function(item) {
    $scope.rankedList.push({
        item: item,
        rank: 0.5 - $window.Math.random()
    });
});

然后命令由像使用领域:

And then order using the field by something like:

<li ng-repeat="i in rankedList | orderBy:'rank'">{{i.item}}</li>

这可以在此的jsfiddle 。

这篇关于在AngularJS 1.2返回'infdig“错误随机排序依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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