Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4 mandatory Input on component

Tags:

angular

I've a component with the following input

@Input() id: string

I would like this input to be mandatory, which means that if I call my component without specifying it, it would throw a js error.

I can do it in the ngOnInit, but I imagine there is a keyword for this ?

like image 856
Scipion Avatar asked Sep 12 '25 18:09

Scipion


1 Answers

You can throw error in ngOnInit method after checking input property:

ngOnInit() {
    if (!id) {
      throw new Error('No ID provided!');
    }
  }
like image 154
Ankit Kapoor Avatar answered Sep 15 '25 16:09

Ankit Kapoor