javascript - Waiting for ng-repeat to finish rendering before attempting to lookup element? -
i have seen this answer , have created similar directive can confirm does fire event, , data associated ng-repeat
does contain data. (at first thought perhaps ng-repeat
rendered once before data set, not seem case.
i need reference element created in ng-repeat
done rendering. selecting class angular.element(".some-class")
. can confirm selector working running line on console after page has loaded.
however, seems event not accurate because @ time event fired there no elements on page.
possibly relevant:
- the
ng-repeat
repeatingng-include
. - the element i'm trying reference highest level element within
ng-repeat
element.
event emitter:
angular.module('onfinishrendermodule', []) .directive('onfinishrender', function ($timeout) { return { restrict: 'a', link: function (scope, element, attr) { if (scope.$last === true) { $timeout(function () { scope.$emit(attr.eventname ? attr.eventname : 'ngrepeatfinished'); }); } } } });
event listener:
var isinitialrender = true; $scope.$on('columnsrendered', function(columnsrendered) { if (isinitialrender) { if (rowisvisible(lastaddedrowindex)) addrow(); else { /* add buffer rows. (we got here because last rendered row not visible meaning previous row 1 of buffer rows.) */ (var bufferiterator = 0; bufferiterator < ($scope.rowpadding - 1); bufferiterator++) addrow(); isinitialrender = false; } } });
methods relevant listener:
var rowisvisible = function(rowindex) { // don't need members. last column of row , check if it's visible. var column = angular.element($scope.columnreference)[rowindex * $scope.elementsinrow]; var columnoffset = { top: column.offset().top }; columnoffset.bottom = columnoffset.top + column.outerheight(); var page = $window; var pagepositions = { top: page.scrolltop() }; pagepositions.bottom = pagepositions.top + page.height(); return ((pagepositions.bottom > columnoffset.top) && (pagepositions.top < columnoffset.bottom)); } var addrow = function() { lastaddedrowindex += 1; for(var = 0; < $scope.elementsinrow; i++) { currentcolumnindex += 1; $scope.columnsinview.push(currentcolumnindex); } }
it seems fact ng-repeat
on ng-include
did make difference.
i added event-emitting directive after ng-incldue
.
from...
<div ng-repeat="columnindex in columnsinview" on-finish-render event-name="columnsrendered"><div ng-include="getcontenturl()"></div></div>
to...
<div ng-repeat="columnindex in columnsinview"><div ng-include="getcontenturl()" on-finish-render event-name="columnsrendered"></div></div>
Comments
Post a Comment