For example, there is an element in the template using a local variable #targetElement
which aims to get its current width whenever it needs. But I don't want to programmatically calculate the style. Tried with a setter with @ViewChild annotation
get: to get the style from the template
set: to set the value to the component
@ViewChild('targetElement')
set imgWidth(content: ElementRef) {
if (content) {
console.log('Current width is: ' + content.nativeElement.clientWidth);
}
}
But it can only get a 0 and it won't update when the page is resized, or any change to the window. So, the question is how I can passively set the #targetElement
into my component in anytime?
Write a setter in ngDoCheck
or ngAfterViewChecked
seems killing the performance.