Angular2 @ViewChild ElementRef offsetHeight always 0
I just added setTimeout()
in my ngAfterViewInit()
function like this:
Simple way:
setTimeout(() => {
// Do what you want here
console.log(this.myElement.nativeElement.offsetHeight);
}, _timeout); // Mine worked even with _timeout = 1
And the output was not zero any-more.
Better way
And 100 percent way that works is:
let offsetHeight = 0;
const refreshInterval = setInterval(() => {
if (offsetHeight === 0) {
offsetHeight = this.giftImage.nativeElement.offsetHeight;
// Do what you want here
console.log(this.giftImage.nativeElement.offsetHeight);
} else {
clearInterval(refreshInterval);
}
}, 10);