Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Gaëtan Redin
Gaëtan Redin

Posted on • Originally published atMedium on

     

How to Handle Required Inputs in Angular

Handling Required Inputs in Angular Directive and Component.

Hey, this article presents tips for handling required inputs in a Directive or a Component.

The classic approach could be implementing the ngOnInit method and throwing errors when the input value is not set.

public ngOnInit(): void {  if (this.myInput === undefined) {    throw new Error('input myInput is required for MyDirective');  }}
Enter fullscreen modeExit fullscreen mode

But I don’t want to have to write this for all my required properties… So yep, I’m a bit lazy … ^^ But the main goal is tosimplify the code.

I recently found an interesting way to do it just once!

Do you know the concept of Typescriptdecorators?

V1 class decorator

That’s what I would like to code:

@Directive(...)@RequiredInputs('input1', 'input2')export class MyDirective {  @Input()  public input1!: unknown;  @Input()  public input2!: unknown;  @Input()  public input3Optional ?: unknown;}
Enter fullscreen modeExit fullscreen mode

Here’s how to implement the decorator which allows doing that:

Now you will get an error for input1 and input2 if it’s not set on the directive using:

<span myDirective></span> // throw Error<span myDirective input1="1"></span> // throw Error<span myDirective input1="1" input2="toto"></span> // No Error
Enter fullscreen modeExit fullscreen mode

As it’s said in comment by a reader you also can handle this with using the compoent/directive’s selector like this:

@Directive({  selector: '[myDirective][input1][input2]',  ...})
Enter fullscreen modeExit fullscreen mode

But the error message will not be consistent. The directive will not be recognized by Angular if you do it like this and that’s all. Whereas here with the decorator you will have an explicit message for the case. So yes there are many ways to handle it, just choose your prefer way.

V2 property decorator

Here’s the code:

You juste have to use it like this:

@Component(...)class MyComponent {  @Input()  @Required()  public myProperty: unknown;  ...}
Enter fullscreen modeExit fullscreen mode

Thanks for reading.

What about you? How do you handle this use case? Please tell me.

Learn More

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Angular developer who love to share. Still seeking quality code and tips
  • Location
    Nantes, France
  • Work
    Lead dev on Angular project
  • Joined

More fromGaëtan Redin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp