diff --git a/lib/index.js b/lib/index.js index 8b28839..b9a8eb9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -70,24 +70,36 @@ angular link: function(scope, elem, attrs) { var container = elem, scrollDistance = angular.isDefined(attrs.scrollDistance) ? parseInt(attrs.scrollDistance) : 0.3, - removeThrottle; + removeThrottle, + rows, + lastChoice; function tryToSetupInfinityScroll() { - var rows = elem.querySelectorAll('.ui-select-choices-row'); - if (rows.length === 0) { - return false; - } + var initialize = function() { + rows = elem.querySelectorAll('.ui-select-choices-row'); + + if (rows.length === 0) { + return false; + } + + lastChoice = angular.element(rows[rows.length - 1]); - var lastChoice = angular.element(rows[rows.length - 1]); + container = angular.element(elem.querySelectorAll('.ui-select-choices-content')); + return true; + } - container = angular.element(elem.querySelectorAll('.ui-select-choices-content')); + if (!initialize()) { + return false; + } var handler = function() { var containerBottom = height(container), containerTopOffset = 0, elementBottom; + initialize(); // re-initialize to prevent losing container element + if (offsetTop(container) !== void 0) { containerTopOffset = offsetTop(container); } @@ -131,4 +143,4 @@ angular }); } } - }]); \ No newline at end of file + }]);