0

I have a component containing a form in its template and also a service injected in the component which would process the form data and handle an HTTP post to a back-end. I would like this service to access an object defined in my component class, since it would contain properties representing values from the form with two-way data binding (aka. banana-in-a-box method).

This stackoverflow post recommends injecting the component as a dependency in the service, but since my service would be injected to the component already, it would create a circular dependency error, so it is clearly not the way to go.

Is my approach correct by having a service for this use case or should I use a different design? If the former, how to implement without circular dependency error?

4
  • You could pass the data from the component into the service via method parameters. Why do you want to complicate things so much? Commented Apr 13, 2022 at 9:21
  • @OctavianMărculescu but how would I access a method from the service in my component? Commented Apr 13, 2022 at 9:31
  • 1
    I advise you to go through this wonderful "Tour of heroes" tutorial that the Angular team built to facilitate the familiarization of new Angular developers and to showcase the most widely used techniques and practices of working with this framework. Commented Apr 13, 2022 at 9:35
  • 1
    @OctavianMărculescu Thanks for the tip, it was useful. Based on the tutorial you provided, I'll try to formulate a solution to my problem above. Commented Apr 22, 2022 at 9:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.