DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • What ChatGPT Needs Is Context
  • What Is JHipster?
  • Never Use Credentials in a CI/CD Pipeline Again

Trending

  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • What ChatGPT Needs Is Context
  • What Is JHipster?
  • Never Use Credentials in a CI/CD Pipeline Again
  1. DZone
  2. Coding
  3. Frameworks
  4. Angular Components: Pass by Reference or Pass by Value?

Angular Components: Pass by Reference or Pass by Value?

When it comes to @Input() and @Output() decorators, which style of passing is it? In this article, we investigate this question.

Dhananjay Kumar user avatar by
Dhananjay Kumar
·
Aug. 15, 18 · Tutorial
Like (6)
Save
Tweet
Share
134.47K Views

Join the DZone community and get the full member experience.

Join For Free

In Angular, you can pass data from a parent component to a child component using the @Input() decorator, and a child component can emit an event to a parent component using the @Output() decorator.

You can learn more about the @Input() decorator and about the @Output decorator here.

The purpose of this blog post is to explain to you whether it is better to pass by reference or pass by value in the context of the @Input() and the @Output decorator.

To start with, let us assume that we have two components, as listed below:

import { Component, Input } from '@angular/core';
@Component({
    selector: 'app-video',
    template: `
        {{data.counter}} {{count}}
     `
})
export class VideoComponent {

    @Input() data: any;
    @Input() count: number;

}

As you see, we have two input properties.

  1. In data property, we will pass an object.
  2. In count property, we will pass a number.

From AppComponent, we are passing the values for both properties, as shown below:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-root',
    template: `
  <app-video [data]='data' [count]='count' ></app-video>
  `
})
export class AppComponent implements OnInit {
    data: any = {};
    count: number;
    constructor() {
    }

    ngOnInit() {
        this.data.counter = 1;
        this.count = 1;
    }
}

As you see, we are passing data (object) and count (number) to the child component. Since data is being passed as an object, it will "Pass by Reference" and, since count is passed as number, it will "Pass by Value."

Therefore, if passing an object, array, or the like, then it is Passed by Reference, and for primitive types like numbers, it is Passed by Value.

To better understand it, let us raise two events on the child component, as shown in the listing below:

import { Component, Input, EventEmitter, Output } from '@angular/core';
@Component({
    selector: 'app-video',
    template: `
        {{data.counter}} {{count}}
        <button (click)='senddata()'>send data</button>
        <button (click)='sendcount()'>send count</button>
     `
})
export class VideoComponent {

    @Input() data: any;
    @Input() count: number;

    @Output() dataEvent = new EventEmitter();
    @Output() countEvent = new EventEmitter();

    senddata() {
        this.dataEvent.emit(this.data);

    }
    sendcount() {
        this.countEvent.emit(this.count);
    }

}

In both events, we are passing back the same @Input() decorated properties to the parent component. In dataEvent, the data is passed back and in countEvent the count is passed back to the parent component.

In the parent component, we are capturing the event as below:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-root',
    template: `
  <app-video [data]='data' [count]='count' (dataEvent)='updateData($event)' (countEvent)='updateCount($event)' ></app-video>
  `
})
export class AppComponent implements OnInit {
    data: any = {};
    count: number;
    constructor() {
    }

    ngOnInit() {
        this.data.counter = 1;
        this.count = 1;
    }

    updateData(d) {
        d.counter = d.counter + 1;
        console.log(this.data.counter);
    }

    updateCount(c) {
        c = c + 1;
        console.log(this.count);
    }
}

Let us talk through the updateData and updateCount functions. These functions are capturing events raised on the child component.

In the updateData function, we are incrementing the value of the passed parameter, however, since it is an object, it will update the value of this.data and in the child component, the updated value will be rendered.

In the updateCount function, we are incrementing the value of the passed parameter, however, since it is a primitive type it will not update this.count and in the child component nothing will happen.

As an output on clicking of button, you will find the value of the data is incrementing but the value of the count is not incrementing.

We can summarize this as follows: if we pass objects in the @Input() decorator then it would be passed as a reference, and if we pass primitive types, then it would be passed as a value. I hope you found this article useful. Thanks for reading!

Pass (software) AngularJS

Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • What ChatGPT Needs Is Context
  • What Is JHipster?
  • Never Use Credentials in a CI/CD Pipeline Again

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: