One issue is that ng-repeat
renders lazily and the link function fires before that process completes. To solve this problem, you can implement a watcher
that evaluates the value of elem[0].offsetHeight
on each digest cycle. This way, when ng-repeat
finishes rendering, it triggers a digest cycle and updates the callback due to the change in the value of elem[0].offsetHeight
.
Custom Directive Example
var app = angular.module('plunker', []);
app.directive('myObject', function(){
return {
restrict: 'A',
link: function(scope, elem){
var calculateHeight = scope.$watch(function(){
return elem[0].offsetHeight;
}, function(newVal, oldVal){
//custom code goes here.
console.log(newVal, oldVal);
calculateHeight(); //deregistering watcher for performance reasons
})
}
};
});
View Demo Here
Another approach is to add another directive to the ng-repeat element which will $emit
an event once it finishes rendering. Then, you can have an event listener inside the directive to calculate the height based on this event.