Angular 19’s Model Inputs: A Simpler Alternative to @Input() & @Output

00:00
BACK TO HOME

Angular 19’s Model Inputs: A Simpler Alternative to @Input() & @Output

10xTeam April 05, 2025 5 min read

Model inputs in Angular 19 are an extension of signal-based inputs, designed to support two-way data binding. They combine an input and an output, allowing components to share state interactively.

Key Features:

  • Writable Signals: Unlike regular signal inputs, model inputs are writable, enabling updates via set() or update().
  • Two-Way Binding: Simplifies scenarios where parent and child components need to share state interactively.
  • Default Values: Can be initialized with default values for optional use cases.
  • Type Safety: Strongly typed, reducing runtime errors.

Example Usage:

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

@Component({
  selector: 'app-checkbox',
  template: ``
})
export class CheckboxComponent {
  checked = model(false); // Model input with two-way binding
}

Benefits:

  • Simplified Two-Way Binding: Eliminates the need for manual event handling and output emissions.
  • Improved Reactivity: Automatically updates dependent computations or effects when the input changes.
  • Type Safety: Ensures that inputs are correctly typed, reducing potential errors.

Comparison with Signal Inputs:

Feature Signal Inputs Model Inputs
Reactivity Read-only signals Writable signals
Two-Way Binding Not supported Supported
Input Transformation Supported Not supported

Migration Considerations

When migrating from traditional @Input decorators to model inputs, consider the following:

  • Type Definitions: Ensure that model inputs are correctly typed to maintain type safety.
  • Event Handling: Use the set() method for updating model inputs instead of emitting events manually.

To define and use model inputs in Angular 19, follow these steps:

2. Defining a Model Input in Angular 19

To create a model input, use the model() function from the @angular/core package. This function defines a writable signal that can be updated and synchronized with parent components.

Example:

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

@Component({
  selector: 'app-custom-slider',
  template: `
    
      -
      
      +
    
  `
})
export class CustomSliderComponent {
  // Define a model input with an initial value of 0
  value = model(0);

  increment() {
    // Update the value and propagate changes to the parent
    this.value.update(oldValue => oldValue + 1);
  }

  decrement() {
    this.value.update(oldValue => oldValue - 1);
  }
}

3. Using Model Inputs in Parent Components

The parent component can bind to the model input using Angular’s two-way binding syntax ([(...)]).

Example:

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

@Component({
  selector: 'app-parent',
  template: `
    
    Parent Value: 
  `
})
export class ParentComponent {
  // Define a writable signal for the slider value
  sliderValue = signal(50);
}

How It Works:

  • The CustomSliderComponent writes updates to its value property.
  • These updates automatically propagate to the sliderValue signal in the parent component.
  • Similarly, changes to sliderValue in the parent reflect in the child component.

4. Default Values and Type Safety

You can provide default values or enforce required values for model inputs:

value = model(10); // Default value of 10

If no default is provided, the input is typed as undefined:

value = model();

5. Advanced Options

Model inputs support additional configuration options like aliases for property names:

value = model(0, { alias: 'sliderValueAlias' });

This allows you to bind with a custom property name:



6. Key Benefits

  • Simplifies two-way binding without manual event handling.
  • Fully reactive and integrates seamlessly with Angular’s Signal system.
  • Strong type safety ensures fewer runtime errors.

Model inputs are a powerful addition to Angular’s reactive programming paradigm, making two-way data binding more intuitive and efficient.

Conclusion

Model inputs in Angular 19 offer a powerful way to implement two-way data binding, enhancing reactivity and simplifying component interactions. By leveraging writable signals and automatic type safety, developers can build more efficient and maintainable applications.

[1] https://medium.com/@davidepassafaro/angular-model-inputs-two-way-binding-inputs-with-signals-b7c31f0f9d14 [2] https://www.w3schools.com/angular/angular_model.asp [3] https://angular.dev/guide/components/inputs/ [4] https://www.simplilearn.com/tutorials/angular-tutorial/angular-forms [5] https://blog.angular-university.io/angular-signal-components/ [6] https://blog.angular-university.io/angular-input/ [7] https://nehalist.io/working-with-models-in-angular/ [8] https://dzone.com/articles/the-ultimate-guide-to-the-input-and-output-decorat-1



Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Audio Interrupted

We lost the audio stream. Retry with shorter sentences?