【发布时间】:2015-10-19 14:46:11
【问题描述】:
我定义了以下指令,它包装了 UI Bootstrap 提供的 pagination 指令
angular.module('my-module')
.directive('myPagination', ['$filter', '$window', function ($filter, $window) {
return {
restrict: 'EA',
scope: {
itemsPerPage: '=',
totalItems: '=',
page: '='
},
link: function (scope, element, attrs) {
scope.hiddenElement = true;
var totalMargin = 200, widthByElement = 41;
scope.getWindowDimensions = function () {
return {
'w': element.width()
};
};
scope.$watch(scope.getWindowDimensions, function (newWidth, oldWidth) {
// calculate how many page buttons fit in the pagination horizontal bar
scope.maxSize = parseInt($filter('number')((newWidth.w - totalMargin) / widthByElement, 0));
}, true);
angular.element($window).bind('resize', function () {
// "$digest already in progress" thrown by the following line
scope.$apply();
});
},
template: '<div class="text-center paginationSelfCare" ng-show="totalItems > itemsPerPage">' +
'<pagination ng-show="totalItems/itemsPerPage <= maxSize" total-items="totalItems" items-per-page="itemsPerPage" ng-model="page" max-size="maxSize" class="pagination pagination-sm" ></pagination>' +
'<pagination ng-show="totalItems/itemsPerPage > maxSize" total-items="totalItems" boundary-links="true" direction-links="true" previous-text="<" next-text=">" first-text="<<" last-text=">>" items-per-page="itemsPerPage" ng-model="page" max-size="maxSize" class="pagination pagination-sm" ></pagination>' +
'</div>'
};
}]);
如果此指令在一个浏览器选项卡 (A) 中运行,并且我切换到另一个选项卡 (B),那么当我切换回 A 时,我会在浏览器控制台中看到以下错误:
错误:[$rootScope:inprog] $digest 已经在进行中 http://errors.angularjs.org/1.4.4/$rootScope/inprog?p0=%24digest minErr/http://127.0.0.1:9000/bower_components/angular/angular.js:68:12 开始阶段@http://127.0.0.1:9000/bower_components/angular/angular.js:16273:1 $RootScopeProvider/this.$gethttp://127.0.0.1:9000/bower_components/angular/angular.js:16014:11 .link/http://127.0.0.1:9000/scripts/directives/pagination.js:30:11
pagination.js 的第 30 行是上面标有注释的行
scope.$apply();
在致电scope.$apply(); 之前我可以检查什么以避免此问题吗?我正在运行 Angular 1.4.4 版
【问题讨论】:
标签: javascript angularjs