Passing values using @ViewChild Angular 4

Passing values using @ViewChild Angular 4:

@ViewChild in angular is mainly to communicate between parent and child components. Because a component will have access only to its own template files. We have few more components like @Input and @Output for the same. Today we are going to see actually how to pass a value within a same component itself using @ViewChild instead of two way databinding.

Steps in Overview:

1. Import ViewChild and ElementRef from @angular/core.
2. Add a decorator @ViewChild with component name / template reference name as a argument to the typescript variable.
3. Access the input text values using nativeElement.value method.

Side Note:

Just print console.log(ElementRef) to see all the available methods of ElementRef, you can print for any component in angular to list its methods and properties.
This simple app just adds technology name and technology description to the array and the same array iterated and printed in the same page. This app is already created using property binding, event binding and two way binding. In the same app I just replaced two way binding with @ViewChild local Reference variable.

 

Have a quick look at:

 

Property binding angular 4 example

 

Angular 4 Event Binding + Two Way Databinding Simple Example

 

 

Component [Typescript File]:

 

  • ViewChild and ElementRef imported from @angular/core.
  • @ViewChild decorator added to variables with reference as a argument. [You can also add the component name].
import { Component,ViewChild,ElementRef } from '@angular/core';
export class AppComponent {
@ViewChild('tech_name') techName: ElementRef;
@ViewChild('tech_description') techDesc: ElementRef;
technologies = [];

addTechName(){
this.technologies.push({
type:'tech',
techname: this.techName.nativeElement.value,
techdesc: this.techDesc.nativeElement.value
});
}
}

 

 

Template


:

 

  • #tech_name and #tech_description’s are the local references.
  • The same only used as a argument in the @ViewChild component.

 

With local Reference: [Used in this post]


<label for="techN"> Enter Technology Name </label>
<input type="text" #tech_name id="techN">

<label for="techD"> Enter Technology Description </label>
<input type="text" #tech_description id="techD">

With two way binding:


<label for="techN"> Enter Technology Name </label>
<input type="text" [(ngModel)] = "techName" id="techN">

<label for="techD"> Enter Technology Description </label>
<input type="text" [(ngModel)] = "techDesc" id="techD">

Now we have replaced two way binding with local reference in this post. But still no difference in our output.

@ViewChild is actually available to use between component connections. Here we learn how to use to pass the input text values to component instead of two way binding.

Output Screenshots:

 

Passing values using @ViewChild Angular 4

 

Passing values using @ViewChild Angular 4

 

348 total views, 2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *