In Angular, I have a reusable autocomplete list and I need to pass the selected option to another component where this custom component is called. However, the custom component is not called directly and there is another component between them as shown below:
Component A: --> Component B: --> Component C:
custom-autocomplete modal-component employee-component
Now there are 2 option I think:
1. I can pass the selected value from Component A to any component via a shared service. But I am not sure if it is a good idea to inject a service dependency to a reusable component.
2. I can pass the selected value to Component B via EventEmitter, but I do not know if it is possible or good idea to pass the emitted event from Component B to Component C. I think I can pass the received event and parameter as I pass via EventEmitter usually, but not sure if it is a good idea.
So, what should be done in this scenario?
In my opinion you should use a shared service in which you should have
BehaviourSubjecttypeobservable. So whenever user will select any value formautocomplerteso you can pass that selected option to yourBehaviourSubjectobservableand then in your anothercomponentyou can subscribe to thatBehaviourSubject.And if you want to use multiple reusable
autocompletein onecomponentso while passing eachautcomplete'sselected value you can pass a property namedtypein which you will be sending yourautocompletetype.Like you have your function like this
So when you call your above function for city so you will be calling in your template like this
And then in your receiving component just do like this
Just gave a rough example to make you understand.