DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

2

Angular v17.1 Signal input function

Image description

Angular 17.1 brings new features to the framework with the introduction (as a developer preview) of Signal inputs.

What is a Signal input? πŸš₯ πŸ€”

We are using the @Input decorator in the Angular project.

Now, we have another option, using the input() function from @angular/core.

This function creates an input value that is received as a signal. Here is an example:

export class SampleComponent {
  name = input<string>('Angular');
}
Enter fullscreen mode Exit fullscreen mode

Since name is a signal, it can be read like so:

<p>hello {{ name() }}</p>
Enter fullscreen mode Exit fullscreen mode

We can derive a value from that input without using ngOnInit or ngOnChanges. Instead, we can use computed:

name = input<string>('Angular');

greeting = computed(() => 'Hello ' + this.name());
Enter fullscreen mode Exit fullscreen mode

The new API also supports required inputs with the input.required function to ensure a value is passed to the component/directive:

export class RequiredComponent {
  firstName = input<string>(); // string | undefined
  lastName = input.required<string>(); // string
}
Enter fullscreen mode Exit fullscreen mode

I hope you found it useful. Thanks for reading. πŸ™

Let's get connected! You can find me on:

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (1)

Collapse
 
spock123 profile image
Lars Rye Jeppesen β€’

Thanks

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay