As mentioned in Angular documentation:
A template reference variable is often a reference to a DOM element
within a template. It can also be a reference to an Angular component
or directive or a web component.
In the following case, the template variable myRef
refers to the component instance:
<my-selector #myRef class="element"></my-selector>
@ViewChild("myRef") myComponent: MyComponent;
If you make the component HTML element available as a public property:
@Component({
selector: "my-selector",
...
})
export class MyComponent {
constructor(private elementRef: ElementRef) { }
public get domElement(): HTMLElement {
return this.elementRef.nativeElement;
}
public doSomething() {
...
}
}
you will have access to it as well as to other public members of the component:
export class ParentComponent {
@ViewChild("myRef") myComponent: MyComponent;
private someMethod() {
let myComponentId = this.myComponent.domElement.id;
...
this.myComponent.doSomething();
...
}
}