← Back to Angular, page 1

angular-0006 · Easy fresher

How do you pass data into and out of a component with input(), output() and model()?

input() declares a signal-based input that the parent sets, output() declares an event emitter the child fires, and model() declares a value that works as a two-way binding. They replace the @Input and @Output decorators in new code.

Full answer

A parent talks to its child through inputs. The child talks back through outputs.

input<T>() creates a read-only signal that is filled by the parent. Variants:

  • input(5) has a default value.
  • input.required<number>() has no default, and the parent must set it. The compiler checks this at build time.
  • input(false, { transform: booleanAttribute }) converts the incoming value, so <app-x disabled /> works.
  • input(1, { alias: 'max' }) changes the public name.

output<T>() creates an emitter. The child calls .emit(value) and the parent listens with (name)="handler($event)".

model<T>() creates a writable signal that also works as an input and an output at the same time. The parent can write [(rating)]="stars", and the child can call rating.set(4) to push the new value up.

Because inputs are signals, you use computed for derived values and do not need ngOnChanges. Reading this.max() inside a computed or template updates automatically.

Legacy: @Input() and @Output() change = new EventEmitter<T>() still work and are common in older code. Signal inputs were introduced in 17.1 and became stable in Angular 19. Details that interviewers often ask about:

  • Input signals are read-only inside the child. To keep a local editable copy that resets when the parent value changes, use linkedSignal(() => this.value()).
  • A required input cannot be read in the constructor, because the parent has not set it yet. The compiler reports this. Read it in a computed, an effect, or ngOnInit.
  • model() can be required too (model.required<number>()). If the parent uses only [rating]="x", it behaves as a plain input. If it uses [(rating)], writes go back up.
  • Output names should describe what happened (saved, selectionChange) and should not start with on, because the parent writes (saved)="onSaved($event)".
  • output() is not an observable, but you can bridge it with outputFromObservable() and outputToObservable() from @angular/core/rxjs-interop.
  • When you create a component in code, set inputs with componentRef.setInput('name', value), so change detection and signals see the change.

For a simple case, a student-card component takes name and marks as inputs and fires selected when the user clicks it. The parent owns the data, the child reports events. This one-way data flow keeps bugs easy to find.

typescript
import { Component, booleanAttribute, computed, input, model, output } from '@angular/core';

@Component({
  selector: 'app-rating',
  template: `
    @for (star of stars(); track star) {
      <button type="button" [disabled]="readonly()" (click)="choose(star)">
        {{ star <= value() ? '*' : '-' }}
      </button>
    }
  `,
})
export class RatingComponent {
  max = input(5);
  readonly = input(false, { transform: booleanAttribute });
  title = input.required<string>();
  value = model(0);
  rated = output<number>();

  stars = computed(() => Array.from({ length: this.max() }, (_, i) => i + 1));

  choose(star: number): void {
    this.value.set(star);
    this.rated.emit(star);
  }
}

@Component({
  selector: 'app-course-page',
  imports: [RatingComponent],
  template: `
    <app-rating title="Angular basics" [max]="5" [(value)]="stars" (rated)="log($event)" />
    <p>You gave {{ stars }} stars</p>
  `,
})
export class CoursePageComponent {
  stars = 3;

  log(n: number): void {
    console.log('rated', n);
  }
}

Angular 20.3. In the parent write `<app-rating [max]="5" [(value)]="stars" (rated)="log($event)" />`.

Key points

  • `input()` and `input.required()` give signals that cannot be written by the child.
  • `output()` replaces `@Output` plus `EventEmitter` for new code.
  • `model()` gives two-way binding with `[(name)]`.
  • `transform` and `alias` are options on `input`.

Common mistakes

  • Reading an input in the constructor. The value is not set yet. Use a `computed`, an `effect`, or `ngOnInit`.
  • Trying to call `set` on an `input()`. Only `model()` is writable by the child.
  • Forgetting the parentheses when reading, so the template shows a function.

Follow-ups

  • Why does `input.required` have no default value?
  • How would you react to an input change without `ngOnChanges`?
  • When would you still use `@Input` today?

Tips

  • Show all three functions in one tiny rating component. Interviewers like a complete round trip.
  • Mention that required inputs are checked by the compiler, not only at runtime.
  • Name the legacy decorators and say they are not removed.

Evaluation rubric

  • Uses input, output, and model correctly.
  • Knows how required, transform, and alias work.
  • Can connect inputs to computed values.