2016-08-23 78 views
0

每当我触发我的作用域上的$apply时,会引发太多的递归错误。

控制台输出:

Error: too much recursion 
[email protected]://.../lib/angular.js:355:10 
[email protected]://.../lib/angular.js:551:9 
[email protected]://.../lib/angular.js:546:23 
[email protected]://.../lib/angular.js:563:28 
... 

Error: 10 $digest() iterations reached. Aborting! 
Watchers fired in the last 5 iterations: [] 
$RootScopeProvider/this.$get</[email protected]://.../lib/angular.js:7756:19 
$RootScopeProvider/this.$get</[email protected]://.../lib/angular.js:7926:13 
+0

用于'AngularJS v1.0.3' – marlo

回答

0

太多递归误差

如果原因通过对angularjs copy方法,你的数据具有圆形参考到彼此。

例如:

// you define your objects 
var lot = new Lot(); 
var car = new Car(); 

// you assign create a relation to it 
car.assign(lot); 
lot.assign(car); 

// lets assume that the data on the objects are like this 
car; // {'lot':lot} 
lot; // {'car':car} 

$apply()被触发,不管你叫它或角调用它,它会调用某个地方到copy方法。

copy会发生什么,它会复制每个属性以及它可以复制的任何内容。在适用于我们上面的数据时,如果不停止,它将永远重演。

这会导致浏览器停止它并引发Too much recursion错误。

解决方案:

仅获得您需要为您的渲染元素的数据。 只传递给需要的数据。

使用上述示例代码,

var reduced = {'plate':car.get_plate(),'lot':car.get_lot_location()}; 
my_scope.addCar(reduced); 

尖端自:在调试时库使用unminified版本。 :)