Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 30099a0

Browse files
committed
fix(pagination): retain model on initialization
- When `total-items` initializes as undefined, ignore value and do not update page information - Remove extra watcher and call action manually Closes #3786 Closes #4783 Fixes #2956
1 parent 167cfad commit 30099a0

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/pagination/pagination.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,16 @@ angular.module('ui.bootstrap.pagination', [])
1616
$scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
1717
self.itemsPerPage = parseInt(value, 10);
1818
$scope.totalPages = self.calculateTotalPages();
19+
updatePage();
1920
});
2021
} else {
2122
this.itemsPerPage = config.itemsPerPage;
2223
}
2324

24-
$scope.$watch('totalItems', function() {
25-
$scope.totalPages = self.calculateTotalPages();
26-
});
27-
28-
$scope.$watch('totalPages', function(value) {
29-
setNumPages($scope.$parent, value); // Readonly variable
30-
31-
if ($scope.page > value) {
32-
$scope.selectPage(value);
33-
} else {
34-
ngModelCtrl.$render();
25+
$scope.$watch('totalItems', function(newTotal, oldTotal) {
26+
if (angular.isDefined(newTotal) || newTotal !== oldTotal) {
27+
$scope.totalPages = self.calculateTotalPages();
28+
updatePage();
3529
}
3630
});
3731
};
@@ -71,6 +65,16 @@ angular.module('ui.bootstrap.pagination', [])
7165
$scope.noNext = function() {
7266
return $scope.page === $scope.totalPages;
7367
};
68+
69+
function updatePage() {
70+
setNumPages($scope.$parent, $scope.totalPages); // Readonly variable
71+
72+
if ($scope.page > $scope.totalPages) {
73+
$scope.selectPage($scope.totalPages);
74+
} else {
75+
ngModelCtrl.$render();
76+
}
77+
}
7478
}])
7579

7680
.constant('uibPaginationConfig', {
@@ -111,7 +115,7 @@ angular.module('ui.bootstrap.pagination', [])
111115

112116
// Setup configuration parameters
113117
var maxSize = angular.isDefined(attrs.maxSize) ? scope.$parent.$eval(attrs.maxSize) : paginationConfig.maxSize,
114-
rotate = angular.isDefined(attrs.rotate) ? scope.$parent.$eval(attrs.rotate) : paginationConfig.rotate;
118+
rotate = angular.isDefined(attrs.rotate) ? scope.$parent.$eval(attrs.rotate) : paginationConfig.rotate;
115119
scope.boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$parent.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
116120
scope.directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$parent.$eval(attrs.directionLinks) : paginationConfig.directionLinks;
117121

src/pagination/test/pagination.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -734,3 +734,27 @@ describe('pagination directive', function() {
734734
});
735735
});
736736
});
737+
738+
describe('pagination directive', function() {
739+
var $compile, $rootScope, element;
740+
beforeEach(module('ui.bootstrap.pagination'));
741+
beforeEach(module('template/pagination/pagination.html'));
742+
beforeEach(inject(function(_$compile_, _$rootScope_) {
743+
$compile = _$compile_;
744+
$rootScope = _$rootScope_;
745+
}));
746+
747+
it('should retain the model value when total-items starts as undefined', function() {
748+
$rootScope.currentPage = 5;
749+
$rootScope.total = undefined;
750+
element = $compile('<uib-pagination total-items="total" ng-model="currentPage"></uib-pagination>')($rootScope);
751+
$rootScope.$digest();
752+
753+
expect($rootScope.currentPage).toBe(5);
754+
755+
$rootScope.total = 100;
756+
$rootScope.$digest();
757+
758+
expect($rootScope.currentPage).toBe(5);
759+
});
760+
});

0 commit comments

Comments
 (0)