-1

I am developing an AngularJs app . when I search about some controller samples like firebase Auth

    .factory("Auth", function($firebaseAuth) {
         var usersRef = new Firebase("https//<YOUR-FIREBASE-APP>.firebaseio.com/users");
         return $firebaseAuth(usersRef);
     })

this will use factory instead of controller. I have changed it to controller and it works as well. I need to know about the difference.

Sajed
  • 445
  • 1
  • 8
  • 19
  • I believe that controller are designed to handle a specific view, while factory are there to build some business services you can reuse throughout your app. Therefore controller are there to make the link between the view and the business logic. But I am not sure if it's really the same with ionic-framework. – nubinub Oct 06 '16 at 08:29
  • please read angular style guide to know more about them. – ram1993 Oct 06 '16 at 08:34
  • Possible duplicate of [AngularJS: Service vs provider vs factory](http://stackoverflow.com/questions/15666048/angularjs-service-vs-provider-vs-factory) – Dexter Oct 06 '16 at 09:08

1 Answers1

1

A factory is used is here because you could use the same Firebase Auth for multiple controllers. Instead, if you are hardcoding this into a specific controller, you would have to do the same for each and every other controllers which need the Auth.(which is bad). Also, it is the cliche that a new feature is added as a Service or Factory.

  • thanks @alan so what is the difference between factory and service? – Sajed Oct 06 '16 at 09:25
  • @SaJed a factory is just a function that returns an instance of a service. When you define a service it just gets wrapped in a default factory that in effect does `return new TheService(args)` so a service needs to expect to be instantiated with 'new' while a factory has to create and return an object. – Duncan Oct 06 '16 at 09:37