How to use Template Reference Variable in Parent-Child components interaction?
Let the Parent component talk to the Child component :)

• Full Stack Engineer • Auth0Ambassador • Organizer at AngularHive community • Speaker • Blogger
Search for a command to run...
Let the Parent component talk to the Child component :)

• Full Stack Engineer • Auth0Ambassador • Organizer at AngularHive community • Speaker • Blogger
I missed your comment somehow. Sorry about that.
I don't think so it is problematic when we use template reference as long as we care about what can be exposed in Component class.
Hello awesome people👋, welcome back! In this post, I am going to walk you through how to set up Tailwind CSS in an Angular project. It is going to be the easiest setup you have ever done. Come on in. This post is for projects that are using Angular ...

Hello awesome people👋, welcome back! This is again a super simple post about Angular, but pretty informative and you might have not read about it so far. Let's begin. What is Text Interpolation? Interpolation refers to embedding expressions or just ...

Hello awesome people👋, welcome back. This blog post is going to be a super simple post, and I just wanna share my TailWind CSS learning with you all. What are we going to design? A simple and minimalistic card component with 100% TailWind CSS. Code...

Hello amazing people👋, welcome back! Introduction If you are from a Java background, you must have used/heard about Apache Tomcat server, which is a default server any Java developer works with to deal with Web app development. The landscape has cha...

Project documentation serves as a guide for new developers in a team. Why not create one for an Angular project?

Writing this blog post right away after implementing this concept in one of my projects. I like documenting my findings as a blog post as it stays here as a reference guide.
Okay, I was trying to invoke methods present in the child component from the parent component so I decided to use the template reference variable.
It was really cool.
Template variables help you use data from one part of a template in another part of the template.
Let's get started.
Let's first create an Angular project.
ng new parent-child-template-ref-var
Go ahead and create a child component called Greet, and add a method called greetMe() to greet Users.
ng g c greet
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-greet',
templateUrl: './greet.component.html',
styleUrls: ['./greet.component.css']
})
export class GreetComponent implements OnInit {
displayTxt: boolean = false;
constructor() { }
ngOnInit(): void {
}
greetMe(){
this.displayTxt = true;
}
}
<div *ngIf="displayTxt">
Hello User!
</div>
When the displayTxt boolean property is true, the text Hello User! is displayed. And this can be done by invoking the method greetMe() as it sets the value true to the property.
greetMe() from Parent componentLook at the #child template reference variable. It does the magic here, has access to all the methods and properties of the greet child component.
<app-greet #child>
</app-greet>
<button (click)="child.greetMe()">
Greet Me
</button>

Take me to Live Demo
If you got any questions, feel free to post them in the comments section down below.
Happy learning!
I tweet a lot about Web development, Java, and Productivity Hacks, so feel free to follow me on Twitter at AskUdhay.
Here is one of my recent tweets: