Kirtesh Shah
How to pass value from template to component and vice versa in Angular?

How to pass value from template to component and vice versa in Angular?

By Kirtesh Shah in Angular on Dec 30 2021
  • Jay Pankhaniya
    Apr, 2023 14

    In Angular, you can pass values between the template and component in a number of ways. Here are a few common ways to do this:

    Property Binding: You can pass data from the component to the template using property binding. This involves binding a component property to a template element property or attribute. For example, to pass the value of a variable named “name” from the component to the template, you could use the following syntax:

    1. <p>Hello, {{ name }}!</p>

    Event Binding: You can pass data from the template to the component using event binding. This involves binding a template event to a component method. For example, to call a method named “handleClick” in the component when a button is clicked, you could use the following syntax:

    1. <button (click)="handleClick()">Click me</button>

    Two-Way Binding: You can pass data back and forth between the component and template using two-way binding. This involves combining property and event binding using the [(ngModel)] directive. For example, to bind an input field to a variable named “username” in the component, you could use the following syntax:

    1. <input [(ngModel)]="username">

    This will bind the value of the input field to the “username” variable in the component, and any changes made to the input field will also update the value of the variable.

    Input and Output Decorators: Another way to pass data between a component and its template is by using the Input and Output decorators. The Input decorator allows you to pass data from a parent component to a child component, while the Output decorator allows you to emit data from a child component to a parent component.
    For example, in the parent component, you could pass a string value to the child component using Input decorator like this:

    1. <child-component [name]="parentName"></child-component>

    And in the child component, you can receive that data using Input decorator like this:

    1. import { Component, Input } from '@angular/core';
    2. @Component({
    3. selector: 'child-component',
    4. template: '<p>Hello {{ name }} from child component</p>',
    5. })
    6. export class ChildComponent {
    7. @Input() name: string;
    8. }

    Similarly, to emit data from a child component to a parent component, you can use the Output decorator in the child component and EventEmitter like this:

    1. import { Component, EventEmitter, Output } from '@angular/core';
    2. @Component({
    3. selector: 'child-component',
    4. template: '<button (click)="onClick()">Click me</button>',
    5. })
    6. export class ChildComponent {
    7. @Output() buttonClicked = new EventEmitter();
    8. onClick() {
    9. this.buttonClicked.emit('Button clicked from child component');
    10. }
    11. }

    And then in the parent component, you can listen to the emitted event and receive the data like this:

    1. <child-component (buttonClicked)="onButtonClicked($event)"></child-component>
    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'parent-component',
    4. template: '<child-component (buttonClicked)="onButtonClicked($event)"></child-component>',
    5. })
    6. export class ParentComponent {
    7. onButtonClicked(data: string) {
    8. console.log(data); // Output: 'Button clicked from child component'
    9. }
    10. }

    These are some common ways to pass data between the template and component in Angular.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS