@ViewChild In Angular

@ViewChild

 
In Angular, if we want to access the template information; i.e, any element from the html file in the ts file or in our component the we can choose the @viewchild concept.
 
By using @viewchild we can achieve the following things,
  • Accessing template of same component
  • Accessing the template of child component
Syntax of creating the @viewchild variable
 
@ViewChild('templaterefvariable’',{static : true}) mytxt : ElementRef
 
In the above @ViewChild will be accepting two arguments and the first is about a string which has to be our template reference variable. My variable name is “mytxt” which is of type “ElementRef”.
 

Accessing template of same component

 
Let’s firstly create our application. By default we’ll be having our app component.
 
App.Component.html 
  1. <div class="row">  
  2.    <input #txtname type="text">  
  3.    <button (click)="MyFunc(txtname.value)" >Click Me</button>  
  4. </div>  
In the above html piece of code, we can find the “#txtname” which is nothing but my template reference variable name. On click of button I passed the parameter as “txtname.value” so that whenever user clicks the button it triggers the “MyFunc” function which will be in our component file.
 
App.Component.ts
  1. import {  
  2.     Component,  
  3.     ViewChild,  
  4.     ElementRef  
  5. } from '@angular/core';  
  6. @Component({  
  7.     selector: 'app-root',  
  8.     templateUrl: './app.component.html',  
  9.     styleUrls: ['./app.component.css']  
  10. })  
  11. export class AppComponent {  
  12.     title = 'Temprefapp';  
  13.     @ViewChild('txtname', {  
  14.         statictrue  
  15.     }) mytxt: ElementRef  
  16.     MyFunc(val) {  
  17.         debugger;  
  18.         console.log(this.mytxt.nativeElement.value);  
  19.     }  
  20. }  
Firstly to get the @viewchild class we need to import the ViewChild from the ‘@agular/core’ library. You can observe “mytxt” variable which is of type “ElementRef” and decorated with @ViewChild. Within the function Myunc function I can access the element value directly by using the ElementRef property named “nativeElement”.
 
By using the viewchild concept we can also access the elements, properties or methods of the child component. In this scenario, let’s take a new component named as Secondcomponent.
 
Now let’s use the secondcomponents selector in the appcomponent as like below, 
  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: 'app-secondcomponent',  
  6.     templateUrl: './secondcomponent.component.html',  
  7.     styleUrls: ['./secondcomponent.component.css']  
  8. })  
  9. export class SecondcomponentComponent {  
  10.     constructor() {}  
  11.     Test() {  
  12.         debugger;  
  13.         return "hello";  
  14.     }  
  15. }  
SecondComponent.html
  1. <p>secondcomponent works!</p>  
Now let’s use this second component in our main appcomponent.html with the help of selector “app-secondcomponent” so that our appcomponent.html looks as like below :
  1. <div class="row">  
  2.    <input type="text"><br/>  
  3. <button >Click Me</button>  
  4. </div>  
  5. <app-secondcomponent></app-secondcomponent>  
Now, let’s make use of @viewchild to access the “Test()” in appcomponent.
  1. import {  
  2.     Component,  
  3.     ViewChild,  
  4.     ElementRef,  
  5.     OnInit,  
  6.     AfterViewInit  
  7. } from '@angular/core';  
  8. import {  
  9.     SecondcomponentComponent  
  10. } from './secondcomponent/secondcomponent.component';  
  11. @Component({  
  12.     selector: 'app-root',  
  13.     templateUrl: './app.component.html',  
  14.     styleUrls: ['./app.component.css']  
  15. })  
  16. export class AppComponent implements AfterViewInit {  
  17.     title = 'Temprefapp';  
  18.     @ViewChild(SecondcomponentComponent, {  
  19.         staticfalse  
  20.     }) vcvariable: SecondcomponentComponent  
  21.     ngAfterViewInit() {  
  22.         debugger;  
  23.         console.log(this.vcvariable.Test());  
  24.     }  
  25. }  
In the above code if we observe, @viewchild was taking the first argument as component name and in the ngAfterViewInint() we accessed the Test().
 
Not only “ngAfterViewInit”, but we can also access the elements info or properties or functions of child component in any of our user defined functions like button click triggering functions.