Skip to content
tezvyn:

Angular's input() Function: Passing Data to Components

Source: angular.devEasyHow cards are made

Angular's input() Function: Passing Data to Components

Angular's input() function lets a parent pass data to a child, defining the child's public API. Use it to configure a component, like passing a value to a slider. The footgun: the returned signal is read-only; the child cannot modify its own input.

Why it exists

Components need a way to receive data from their parents to be reusable. Hardcoding data inside a component limits its use to one specific context. The input function provides a formal, type-safe contract for passing data down the component tree, making components configurable and dynamic.

The mental model

An input is a one-way data channel from a parent component to a child. Think of it as setting the initial properties or "props" of a component when you use it in a template. The parent owns the data; the child receives a read-only signal representing that data. This is the modern, signal-based replacement for the older @Input() decorator.

How it works

You declare an input by initializing a component property with the input() function from @angular/core. For example, value = input(0); creates a number input with a default value of 0. The function returns an InputSignal, which is a read-only signal. To get the current value, you call the property as a function: this.value(). You can make an input mandatory with input.required<Type>(), which causes a build error if the parent doesn't provide a value in the template. You can also provide a transform function to process the value, like trimming a string, before it's used in the component.

When to use it

Use inputs whenever a child component needs data from its parent to render or perform its logic. This is the primary mechanism for top-down component communication. Use it to pass a user object to a UserProfileComponent, an array of items to a ListComponent, or a configuration object to a ChartComponent.

When not to use it

Do not use inputs for a child to send data up to a parent. For that, use @Output() and event emitters. Also, avoid using inputs for a child to directly modify the data it receives. The data flow is one-way (down). If a change is needed, the child should notify the parent to make the change, which will then pass the new value back down through the input.

One canonical example

A parent component wants to set the starting value of a custom slider component.

Parent Template: <custom-slider [value]="50" />

Child Component (custom-slider.ts):

import { Component, input } from '@angular/core';
@Component({ selector: 'custom-slider', ... })
export class CustomSlider {

value = input(0); // Declares a number input with a default of 0 // To read it inside a method: console.log(this.value()); }

Here, the CustomSlider component receives the value 50 from its parent. If the parent hadn't provided a value, it would default to 0.

Interview question

Which statement accurately describes data passed to an Angular component using the input() function?

  • a.It is primarily designed for the child component to send data back to its parent.
  • b.The child component can directly update the input value, and the parent will be notified.
  • c.It establishes a two-way data binding, allowing both parent and child to modify the value.
  • d.The child component receives a read-only signal, preventing it from directly modifying the data.Correct
Why?

The card explicitly states that the `input()` function returns a "read-only signal" and that "the child cannot modify its own input." This enforces a one-way data flow from parent to child, making options suggesting two-way binding or child modification incorrect.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles