简体   繁体   中英

Angular Service Factory Factory

My understanding of angular services is that they are used to access external data sources amongst other things.

So let's assume I have a service to access feeds, that deals with the ATOM parsing, etc.

Now, let's assume a controller needs to access several feeds.

Is there a way for me to parameterize services as they are instantiated? Since services are singletons, do I need a service factory factory? Should I be using the same service and passing details of the particular feed each time? What if I need to make more than one calls to the same feed and would like a dedicated object to speak with? (think websockets instead of feeds).

Is there another approach altogether that would work for this?

Is there a way for me to parameterize services as they are instantiated?

Not really. You can inject things into a service – eg, another service – but I don't think that will help you here.

Since services are singletons, do I need a service factory factory?

I don't know how you would write that, but again, I don't think it would help here.

Should I be using the same service and passing details of the particular feed each time?

Well, as I asked in the comments, if you are dealing with a fixed set of feeds, I would hard-code them into the service (or maybe have the service fetch them from a configuration file on the server), and allow the controller to ask for them by name or some ID.

If you need something more dynamic, then I think you'd have to pass in the feed details to the service.

In either case, I think one "atomFeed" service would be sufficient.

What if I need to make more than one call to the same feed and would like a dedicated object to speak with?

I would probably still use one service. I'm not sure what the issue is here though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM