![](/img/trans.png)
[英]Triggering an alert if the element that is clicked matches the array item in my random function
[英]Random function triggering $digest() error
我正在開發我的第一個角度應用程序,並遇到以下錯誤:
未捕獲的錯誤:達到10個$ digest()迭代。 中止!
我認為我已經隔離了使用random()函數的源。 就我所讀到的有關此錯誤的內容而言,這似乎很有意義( https://github.com/angular/angular.js/issues/705 )。 但是,我不知道如何避免此錯誤。
我使用以下簡單代碼重新創建了錯誤:
randtest.js:
function FirstCtrl($scope) {
$scope.randNum= function(){
var rando = Math.floor(Math.random()*100);
return rando;
};
}
的index.html
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="randtest.js"></script>
</head>
<body>
<div ng-controller="FirstCtrl">
<h1>{{randNum()}}</h1>
</div>
</body>
</html>
不要將$ scope屬性直接設置為該函數。 讓scope屬性引用函數的結果:
var r = function(){
var rando = Math.floor(Math.random()*100);
return rando;
};
$scope.randomNum =r();
HTML:
<h1>{{randomNum}}</h1>
更新 -不將計算包裝在函數中也似乎避免了該錯誤:
$scope.randomNum = Math.floor(Math.random()*100);
錯誤發生的原因超出了我...
當您在屬性上創建更改循環時,通常會發生您提到的錯誤。
例如,就像當您監視某個屬性的更改,然后在偵聽器上更改該屬性的值時。 確保您的代碼沒有這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.