简体   繁体   English

使用HTML5历史记录API会导致angularjs中发生未知错误

[英]using the HTML5 history API causes a unknown error in angularjs

in my angularjs based application, I am using the HTML5 history API to record the history of links I have clicked. 在基于angularjs的应用程序中,我正在使用HTML5历史记录API记录单击的链接的历史记录。

 history.pushState(null, null, '/meeting/site/');

whenever I put this line of code in the angularjs, the console will emit a error message 每当我将这段代码放在angularjs中时,控制台都会发出错误消息

Error: [$rootScope:infdig] http://errors.angularjs.org/1.2.7/$rootScope/infdig?p0=10&p1=%5B%5B%22fn%3A…ce%3D!1%3Breturn%20l%7D%3B%20newVal%3A%20210%3B%20oldVal%3A%20209%22%5D%5D
    at Error (<anonymous>)
    at http://localhost/js/vendor/angular.min.js:6:449
    at h.$digest (http://localhost/js/vendor/angular.min.js:101:152)
    at h.$apply (http://localhost/js/vendor/angular.min.js:103:100)
    at http://localhost/js/vendor/angular.min.js:74:474 

I have no idea what causes this error and why ? 我不知道是什么原因导致此错误,为什么?

The infdig error means "Infinite digest", which means that the digest cycle never stops. 输入错误的意思是“无限摘要”,这意味着摘要循环永远不会停止。 To understand what that means, you need to understand how the digest cycle works. 要了解这意味着什么,您需要了解摘要循环的工作方式。

Angular is based on "dirty checking", and a digest cycle is when Angular iterates over all properties on the scope to see which has changed. Angular基于“脏检查”,摘要循环是Angular遍历示波器上的所有属性以查看哪个属性已更改的时候。 If any properties has changed, it fires all watches for those properties to let them know that something has happened. 如果任何属性已更改,它将触发所有属性的监视,以让他们知道发生了什么。 And since a watch can change properties on the scope, Angular runs another round of dirty checking after the watches are done. 而且由于手表可以更改示波器的属性,因此在完成手表后,Angular会进行另一轮脏检查。 The digest cycle stops when it has iterated over all properties and it sees that none of them has changed. 当摘要循环遍历所有属性并且发现它们都没有更改时,摘要循环将停止。 An infinite digest occurs when a watch is always setting a new value to a property. 当手表始终为属性设置新值时,将发生无限摘要。 Something like this might explain it better: 这样的事情可能会更好地解释它:

$scope.myNumber = Math.random();
$scope.$watch('myNumber', function() {
  $scope.myNumber = Math.random();
});

That is, our watch will never stop being called, since it's always changing the value of myNumber . 也就是说,我们的手表永远不会停止被调用,因为它总是在更改myNumber的值。 Another common cause of the error is when you have two properties and two watches, like this: 另一个常见的错误原因是当您有两个属性和两个手表时,如下所示:

$scope.prop1 = Math.random();
$scope.prop2 = Math.random();

$scope.$watch('prop1', function() {
  $scope.prop2 = Math.random();
});
$scope.$watch('prop2', function() {
  $scope.prop1 = Math.random();
});

Those watches will trigger each other in an endless loop. 这些手表将在无尽的循环中相互触发。

Now, this doesn't explain why you get the error, but I'm guessing you haven't told Angular that you're using the History API and not the default hashbang. 现在,这并不能解释为什么会出错,但是我想您还没有告诉Angular您使用的是History API,而不是默认的hashbang。 You do that by calling $locationProvider.html5Mode(true); 您可以通过调用$locationProvider.html5Mode(true); . Something like this: 像这样:

myApp.config(function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider
    .when('/page1', { template: 'page1.html', controller: 'Page1Ctrl' })
    .when('/page2', { template: 'page2.html', controller: 'Page2Ctrl' })
});

You shouldn't call history.pushState() directly, but use Angulars $location service instead, which will let Angular know when you're modifying the location. 您不应直接调用history.pushState() ,而应使用Angulars $location服务,当您修改位置时,它将使Angular知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM