0

I am using Angular and firebase for the backend to build an app. Many of my controllers will be working with firebase in someway. Should I just keep a ref to my firebase instance in the $rootScope or create a new one in each controller.

    //at the top of each controller?        
    var ref = new Firebase(firebaseUrl);

Or

    //set this once in app launch and use everywhere else
    $rootScope.fbRef = new Firebase(firebaseUrl);
Stradosphere
  • 1,285
  • 3
  • 14
  • 40

1 Answers1

0

Is "neither" an acceptable answer? It's more common to create a data service (store) and inject that service into controllers as needed.

Rodney G
  • 4,746
  • 1
  • 16
  • 15
  • Ok that makes sense, and a service is only initialized once, then that instance is injected into each controller. Or do I have that backwards with a factory? – Stradosphere Aug 08 '15 at 01:03
  • Services and factories both are singletons in Angular, and both must be injected into the controllers where you want to use them. Good explanation here: http://stackoverflow.com/a/21900284/1072176 – Rodney G Aug 08 '15 at 01:21