The ViewChild
decorated variable elementView
is only set after the ngAfterViewInit
life cycle hook:
import {Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './src/app.html'
})
export class AppComponent implements AfterViewInit {
@ViewChild('content') elementView: ElementRef;
contentHeight: number;
constructor() {
}
ngAfterViewInit() {
this.contentHeight = this.elementView.nativeElement.offsetHeight;
}
}
See https://angular.io/api/core/ViewChild#how-to-use for more info.