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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why AI Testing Needs Confidence Scores, Not Just Pass/Fail Results
  • When Data Quality Checks Pass but the Data Is Still Stale
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users

Trending

  • Full-Text Search Meets the Graph: Apache Lucene Inside Neo4j
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces
  • Genkit Flows on Kubernetes, Routed by a Decision Model: senro + jev in Go

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
Dhananjay Kumar
·
Aug. 15, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
140.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why AI Testing Needs Confidence Scores, Not Just Pass/Fail Results
  • When Data Quality Checks Pass but the Data Is Still Stale
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook