0

I am making a factory with multiple functions. Structure is like following:

app.factory('Service', function() {

    var Service = {};
        Service.method1 = function($scope) {
        //logic
        return something1
        };
        Service.method2 = function($scope) {
        return something2
        };
        Service.method3 = function($scope) {
        return something3
        };
        Service.method4 = function($scope, Service) {
        var object1 = Service.method1($scope)
        var object2 = Service.method2($scope)
        var object3 = Service.method3($scope)

        //do something with object 1,2,3 and return the result
        return result
        };

Is this a right way of using a function in a factory inside other function in a factory? And I'm not sure whether I can just pass $scope like that.

4
  • Does this work for you? Are you getting any errors or anything? Take a look at this question: stackoverflow.com/q/22159189/1887101 Commented Apr 26, 2016 at 19:33
  • 3
    Generally you don't want to use scope in your service. $scope binds data to the view, which is the job of the controller. The service should just handle data for the controller to use, just as results from a $http request. Commented Apr 26, 2016 at 19:35
  • The code works only where the data lives which is controller1. But when I use this service in controller2, it does not work. Is there a way to persist the data within the service? I know it's not a good practice to pass $scope, but all my data lives in it so I'm not sure what is the best way Commented Apr 26, 2016 at 19:40
  • Services are singletons so the data will persist once it's assigned. I created a fiddle example here: jsfiddle.net/koa23fb5 Commented Apr 26, 2016 at 19:47

2 Answers 2

0

Answer for Question 1

You can call an internal service function from self

var moduleA= angular.module('A',[]);
moduleA.factory('Service', function() {
    var self = this;
    self.method1: function() {
            alert('a');
        },
    self.method2: function() {
            self.method1();
        } 
    };
});

Then you can call your factory service like this

angular.module('B',['A']).controller('BCtrl',function($scope,'Service'){
    Service.method2();
});

Answer for Question 2

A service should not change any $scope. You don't typically use $scope inside a factory, service or provider. However, you can inject $rootScope dependency in the factory's function constructor and use it.

module.factory( 'Service', function($rootScope){
    $rootScope.value = "value";
});
Sign up to request clarification or add additional context in comments.

1 Comment

can you provide more info on self and this? do I name self as my service name? or is it literally self and this?
0

Factories need to return the service object. Persistent data is declared as var's in the factory function.

app.factory('Service', function() {

    //Declare persistant data here
    var persistantData;

    var Service = {};

    Service.method1 = function(arg) {
        //logic
        return something1;
    };
    Service.method2 = function(arg) {
        return something2;
    };

    //return service object
    return Service;
};

USAGE:

app.controller('ctrl1', function($scope, Service) {
    $scope.out1 = Service.method1($scope.in1);
});

Anything stored in persistent data will last the entire lifetime of the app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.