我使用angular 1.3编写了一些代码来加载来自api的嵌套注释。当只有一些注释时,事情会很好,但是它会中途停止,当有很多注释时我在控制台中出现以下错误:
10 $digest() iterations reached. Aborting!
无论加载的评论数量多少,我都会得到相当明显的滞后。 如果有人可以指出我做错了什么,我会非常感兴趣。
以下是我的一些代码:
<!-- View2.html -->
<script type="text/ng-template" id="myTemplate">
<li ng-init="show=true">
<span ng-click="show = !show" ng-show="show"> HIDE COMMENT </span>
<span ng-click="show = !show" ng-show="!show"> SHOW COMMENT </span>
<strong> posted: </strong> {{ View2.getPostAge(comment.data.created_utc * 1000) }}
<span ng-show="show">
{{ comment.data.body }}
<ul ng-show="{{comment.data.replies}}" ng-repeat="comment in comment.data.replies.data.children" ng-include="'myTemplate'" class="comment-reply"></ul>
</span>
</li>
</script>
<div>
<ul ng-repeat="comment in View2.postItems" ng-include="'myTemplate'">
</ul>
</div>
View2.js
(function(){
'use strict';
var View2Ctrl = function($scope, $stateParams, $timeout, getPageJsonService){
//console.log($stateParams.link);
this.currentDate = (new Date).getTime();
//
this.sub = $stateParams.link; //passed by view1
var parent = this;
this.loadPage = function(){
var self = this;
this.sub = parent.sub;
this.postItems = {};
$timeout(function(){
getPageJsonService.getPage(self.sub)
.then(function(response){
// self.postItems = response.data.data.children;
self.postItems = response.data[1].data.children;
// response.data.data.children;
//tests
console.log(response.data[1].data.children);
});
}, 10);
};
this.loadPage();
this.viewComments = function(url){
$state.go('view2', {link: (url)});
}
this.getPostAge = function(utc_date){
var milliseconds = parent.currentDate - utc_date;
var seconds = Math.round( milliseconds/1000 );
var minutes = Math.round( seconds/60 );
var hours = Math.round( minutes/60 );
var days = Math.round( hours/24 );
var months = Math.round( days/30 );
var years = Math.round( months/12 );
if(years != 0){
return(years + "years ago.");
}else if(months != 0){
return(months + "months ago.");
}else if(days != 0){
return(days + "days ago.");
}else if(hours != 0){
return(hours + "hours ago.");
}else if(minutes != 0){
return(minutes + "minutes ago.");
}else if(seconds != 0){
return(seconds + "seconds ago.");
}
return("days:" + Math.round(days) + "hours:" + Math.round(hours));
}
//
}
angular
.module('myApp.view2', [])
.config(['$stateProvider', function($stateProvider) {
$stateProvider
.state('view2', {
params: {link: null},
url: '/view2',
templateUrl: 'view2/view2.html',
controllerAs: 'View2',
controller: 'View2Ctrl'
})
}])
.controller('View2Ctrl', [
'$scope',
'$stateParams',
'$timeout',
'getPageJsonService',
View2Ctrl
]);
})();