Original Answer
In fact, if you want a component or directive to be aware of the specifics of its host or a parent element, you need an ElementRef, and the way to pass that reference to the component is via Dependency Injection.
Consider a component called SomeParentComponent and this other component, which is child of it:
@Component({
selector: 'some-child'
..
})
export class SomeChildComponent {
constructor( @Self() private element: ElementRef ) {}
}
By using the @Self decorator and injecting an ElementRef in the constructor, you get a reference to the <some-child> element, and so you can query for specific properties of the element.nativeElement like offsetHeight and offsetWidth.
Beware, however, when you query for those properties, since if you have projected content using <ng-content> inside the template of some-child, or you're instantiating another component, you will get different values for those properties in different lifecycle hooks, since for instance, projected content only exists from ngAfterContentInit onwards.
You can also get a reference of the parent component, or any component up in the component tree, by injecting it in the constructor:
@Component({
selector: 'some-child'
..
})
export class SomeChildComponent {
constructor( @Host() private someParent: SomeParentComponent ) {}
}
This way you can access the class definition of SomeParentComponent, use and even modify its public properties.
Even though both @Host and @Self decorators use is kinda optional, it makes clearer what you're passing as a dependency to your component.
Edit 11/17/2017
In order to access the properties of your injected ElementRef there's a couple of alternatives, you could use value accessors or just a plain class method:
@Component({...})
export class SomeChildComponent implements OnInit {
// Using a getter accesor for each property
get height(): number {
return this.element.offsetHeight;
}
get width(): number {
return this.element.offsetWidth;
}
// Using a getter accesor for both properties
get dimensions(): { width: number, height: number } {
return { width: this.element.offsetWidth, height: this.element.offsetHeight }
}
constructor( @Self() private element: ElementRef ) {}
ngOnInit() {
console.log( this.height ); // this.element.nativeElement.offsetHeight
console.log( this.width ); // this.element.nativeElement.offsetWidth
console.log( this.dimensions ); // { width: this.element.offsetWidth, height: this.element.offsetHeight }
console.log( this.getDimensions() ); // { width: this.element.offsetWidth, height: this.element.offsetHeight }
}
// Using a class method
getDimensions() {
return { width: this.element.offsetWidth, height: this.element.offsetHeight }
}
}