AI Hints
Home›Angular Tutorial›Parent and Child, input() and Required Inputs and Defaults
Angular · Lesson 47 of 120

input()

Data from the parent.

⏱ 2 min read▶ Video at 56:02🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

An input is a property, that the parent fills in. It's how data travels down, from parent to child.

  • A property
  • The parent fills it
  • Data travels down

Declaring inputs

In the child, input creates an input, with a default value. It's a signal, so we read it with brackets. The parent sets it in the template, like an HTML attribute. Square brackets pass a value from code, and no brackets pass plain text.

keyvalue
label = input('Price')with a default
Readlabel()
[price]="salePrice()"a value from code
label="Sale price"plain text

price-tag.ts

Price tag has two inputs. Label defaults to the word price, and price defaults to zero. The template reads them, like signals.

app.tstypescript
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-price-tag',
  template: `<p class="card">{{ label() }}: <b>£{{ price() }}</b></p>`,
})
export class PriceTag {
  label = input('Price');
  price = input(0);
}

Using the inputs

The first tag gets a price, but keeps the default label. The second gets both, with the label as plain text, and the price from a signal. The third gets nothing, so both defaults show. Run it. Each tag shows exactly what its parent gave it.

app.tstypescript
import { Component, signal } from '@angular/core';
import { PriceTag } from './price-tag';

@Component({
  selector: 'app-root',
  imports: [PriceTag],
  template: `
    <app-price-tag [price]="9.99" />
    <app-price-tag label="Sale price" [price]="salePrice()" />
    <app-price-tag />
  `,
})
export class App {
  salePrice = signal(7.49);
}
Also uses price-tag.ts
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-price-tag',
  template: `<p class="card">{{ label() }}: <b>£{{ price() }}</b></p>`,
})
export class PriceTag {
  label = input('Price');
  price = input(0);
}

Summary

Input declares a property the parent can set, with a default. It's read like a signal. Next, required inputs.

Key takeaways
  • input('Price')
  • [price]="..."
  • Defaults
Next up: Required Inputs and Defaults — inputs you must give