0

Is it possible (and a good idea) to set a model (scope vars) in a factory service?

For example, I have a factory which defines a model below how can I now init this in the controller?

(function () {
    'use strict';
    angular.module('services.auth', [])

        .factory('AuthorisationService', function ($rootScope, $http) {

            // Public variables
            var authService = {
                user: {
                    email: '',
                    password: ''
                }

            };

            return authService;

        });


})();

controller:

.controller('LoginCtrl', ['$scope', 'AuthorisationService', function ($scope, AuthorisationService) {

     AuthorisationService();



            };


        }]);
Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • 1
    Why not just expose a method in your factory that returns an object and assign that result to the controller? Or use a service instead. – ryanyuyu Jan 07 '16 at 13:49
  • I'm using a factory as it's my understanding later this will be better for a whole login system. In terms of a method, you mean have a function inside the factory then assign it to the scope in the controller? – Prometheus Jan 07 '16 at 13:52
  • 1
    Yes. Expose the methods (like an API) and then in the controller do something like `$scope.value = factory.function()` – ryanyuyu Jan 07 '16 at 13:53

3 Answers3

1

You can pass the scope of the controller to the factory as a parameter and then set values on that scope. The answer to the question if this is a good idea: rather not. It usually isn't best practice to do so. Better change your variables of your scope in your controller only and if you needs variables in your service then those variables should be set there and be used from the controller via getters/setters.

Patrick Kelleter
  • 2,631
  • 1
  • 13
  • 19
1

Sure you can do it that way, I love to use a service and keep the controller clean.

Take a look at this demo:

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope, AuthorisationService) {
  $scope.authService = AuthorisationService.authService;
});

app.factory('AuthorisationService', function($rootScope, $http) {
  var AuthorisationService = {};

  // Public variables
  var _authService = {
    user: {
      email: 'test@gmail.com',
      password: 'test123'
    }
  };

  AuthorisationService.authService = _authService;
  return AuthorisationService;
});
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <p>Email: {{authService.user.email}}</p>
  <p>Password: {{authService.user.password}}</p>
</body>

</html>

If you have any more questions about this just let me know!

RiesvGeffen
  • 1,539
  • 2
  • 11
  • 29
  • Cool thats works, without asking a whole new question in this example does a ``service factory`` make sense over a ``service service``? I will be added a login function to it plus a is_auth function – Prometheus Jan 07 '16 at 14:01
  • 1
    @Spaceships For more information about `angular.service` and `angular.factory` take a look over [here](http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory). Sure you can make as many functions as you want in this factory, but what I like to do is keep it clean and make a new factory when I'm about to make new function about another subject. – RiesvGeffen Jan 07 '16 at 14:04
  • so I can expose services linked to many factories and in the controller use the service like an API? – Prometheus Jan 07 '16 at 14:07
  • @Spaceships I have many factories and use them in the controller kinda like an API yes. Just exactly how I did in the demo. I make an object in the factory and insert every function, object, array etc... in the main object so I can use it everywhere I want. – RiesvGeffen Jan 07 '16 at 14:10
0

If u want to share data between ur controllers u can use tat.otherwise if the data does not required for any other controllers better declare variables(public/privately) inside controller itself.

sundar
  • 398
  • 2
  • 12