0

I have 2 Angular apps in my web app, and I need to have a service declared in a separate file, which to be used in both apps, so I do not want it to be bound to any specific app. I do not care if it will be one instance of service per app or single common instance, I just do not want to duplicate my service code.

How can I declare service outside of app and then bind it to specific app? All examples of services/factories declarations I saw started from module reference like module.service(...).

YMC
  • 4,925
  • 7
  • 53
  • 83
  • 1
    I think this post relates to your issue: http://stackoverflow.com/questions/16725392/share-a-single-service-between-multiple-angular-js-apps – Manube Mar 26 '15 at 14:59
  • This post about sharing single instance of the service between 2 apps, but yes, it shows proper syntax to share code too. Thanks – YMC Mar 26 '15 at 15:28

1 Answers1

1

You can place your service in a third, separate module and then have your original 2 modules depend on the new, third service module.

Essentially:

Angular.module("serviceModule",[])
    //....your service code

Angular.module("App1", ["serviceModule"])
    //....App1 code that can use the service
Angular.module("App2", ["serviceModule"])
    //....App2 code that can use the service
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122