2015-10-29 17 views
0

在這個小提琴(http://jsfiddle.net/edwardtanguay/6pn8tb83/1),角增量一批每一秒,但是當一個警告窗口彈出,計數停止,當我關閉警告窗口,我得到的錯誤如何在警報彈出窗口可見時使Angular增加計數器?

Error: [$rootScope:inprog] $apply already in progress http://errors.angularjs.org/1.2.1/ $rootScope/inprog?p0=%24apply

該錯誤鏈接實際上帶我到一個似乎解釋我需要做什麼的頁面,但由於我沒有明確使用$scope.$apply()$scope.$digest(),我不明白我需要做什麼,以便Angular只是繼續增加並顯示遞增的數字同時彈出警報窗口。

<div ng-controller="MyCtrl"> 
    <div>Angular is counting: {{counter}}</div> 
    <button ng-click="processFiles()">processFiles</button> 
    <div>{{message}}</div> 
</div> 

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

function MyCtrl($scope, $interval) { 

    var theTimer = $interval(function() { 
     $scope.counter++; 
    }, 1000);   

    $scope.counter = 0; 
    $scope.message = 'click button'; 

    $scope.processFiles = function() { 
     alert('ok'); 
    } 
} 
+2

在我的瀏覽器計數器停止時,當我看到警報但關閉警報後沒有得到任何錯誤。 – Manwal

+0

我認爲你應該使用比alert更友好的東西,並且它可以解決你的問題。請參閱Bootstrap的模塊,例如 – Aaron

+0

是的,如果我單擊快速關閉的警報框,我也不會收到錯誤消息,但如果在關閉警報之前等待幾秒鐘,您將看到錯誤消息,至少我在Firefox做。 –

回答

0

這是默認的行爲。瀏覽器對話框打開時腳本執行停止。 解決方法是獲取警報和更新計數器之間的時差,另一個是創建自己的模式

相關問題