Troubleshooting NG0950: Input is required but no value is available yet
4 minutes read
••
#articles#angular#troubleshooting
Introduction
When working with Angular components, you may encounter the NG0950: Input is required but no value is available yet error.
This error occurs when an @Input() or input() property is used before the framework has a value available for it.
The error
Let’s consider the following code:
At line 4, we’re using the input.required method to mark the control property as required. This means that the control input must be provided by the parent component.
At line 6, we’re using the control property to create an observable that fetches a list of clients based on the user’s input.
This code is concise and easy to understand, but if we run it, we’ll get the following error:
A possible solution
As stated in the official Angular documentation, the trick is to move the clients property initialization inside the ngOnInit lifecycle hook.
This change ensures that the control property is accessed after in the ngOnInit lifecycle hook, when the value is available.
However, this will raise another error:
The final solution
To fix every error, we need to modify a little bit the template.
At line 6, we’re using the input event to update the query signal when the user types something new.
At line 5, we introduce a new query signal that will be updated from the template.
At line 8, we convert the signal to an observable so that we can have a stream of values to work with.