我已經定義了下面的指令它包裝如果這個指令在一個瀏覽器選項卡(A)上運行的UI引導
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>'
};
}]);
提供的pagination
指令,和我切換到不同的選項卡(B),然後當切換回A,I看到在瀏覽器控制檯以下錯誤:
Error: [$rootScope:inprog] $digest already in progress 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 [email protected]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
是否有一個原因,我不應該總是使用'$ scope。$ applyAsync()'而不是'$ scope。$ apply()'? – ivanhoe