在我们非常庞大且非常复杂的AngularJS应用程序中,我注意到(偶然!)在我的主模块设置中这样的一行……
application.run(function($rootScope) { window.setInterval( () => { $rootScope.$digest(); },1000); });
…在$http.post请求的激活时间内有显着的积极影响.
我们的代码的一小部分确定性地再现了这种行为:
// In the partial <button ... ng-click="buttonHandler" ...> // In the controller $scope.buttonHandler = function() { $http.post(....).success( function() { console.log("Activated"); }) }
>我们将按钮的ng-click处理程序与我们的一个Web服务的调用相关联.
> Web服务本身在30ms内响应(根据Chrome开发人员工具).
>但是,.success处理程序中的代码在1.75 – 2.3秒后执行(然后才会在控制台中显示“已激活”消息).
然而,当我们将永恒的rootScope $digest Hack(TM)放到位时,激活发生在不到一秒钟!