DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

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:

Top comments (1)

Collapse
 
spock123 profile image
Lars Rye Jeppesen

Thanks