2016-08-23 39 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版本。 :)