0

I have an angular controller which have a number of variables decalred. I am trying to access these in my angular service:

app.controller("MyController", function($scope, myService) {

    var pageIDs = {
        '1': 'home',
        '2': 'contact-us',
        '3': 'products',
        '4': 'complaints'
    }

    var doSomething = {
        'home': function () {
            $scope.Model($scope.MyData, {
                Data: $scope.Data
            });
        },
        // Contact-us function ////////
        // Products functions ////////
        // Complaints functions ////////
    }

    $scope.service = myService.getData;
}

app.factory('myService', function() {
    return {
        getData: function() {
            var Hash = window.location.hash;
            if (Hash) {
                var WithoutHash = WithHash.substring(1);
                if (doSomething [WithoutHash]) doSomething [WithoutHash]();
            }
        }
     };
 });

As you can see, within myService, i am trying to access the var doSomething that is defined within my controller.

1
  • 1
    I think you should change your approach, service/factory has no clue where is it injected, what if you inject myService to 10 controllers and each of them will have doSomething? Treat services as helpers for controllers or a collection of functionalities that can be extracted from controller and work on it's own or as a common part for few controllers. Hope that makes sense Commented Sep 3, 2014 at 10:31

2 Answers 2

1

You can give the variable to the service:

$scope.service = myService.getData(doSomething);

and in your service:

app.factory('myService', function() {
    return {
        getData: function(doSomething) {
            var Hash = window.location.hash;
            if (Hash) {
                var WithoutHash = WithHash.substring(1);
                if (doSomething [WithoutHash]) doSomething [WithoutHash]();
            }
        }
    };
});
Sign up to request clarification or add additional context in comments.

Comments

1

put all the variable in $scope or any object and pass service method like this :

app.controller("MyController", function($scope, myService) {

            $scope.pageIDs = {
                '1': 'home',
                '2': 'contact-us',
                '3': 'products',
                '4': 'complaints'
            }

            var doSomething = {
                'home': function() {
                    $scope.Model($scope.MyData, {
                        Data: $scope.Data
                    });
                },
                // Contact-us function ////////
                // Products functions ////////
                // Complaints functions ////////
            }

            $scope.service = myService.getData;
            myService.setData($scope);

        }

        app.factory('myService', function() {
            var controllerVar = {};
            return {
                getData: function() {
                    var Hash = window.location.hash;
                    if (Hash) {
                        var WithoutHash = WithHash.substring(1);
                        if (doSomething[WithoutHash]) doSomething[WithoutHash]();
                    }
                }
                setData: function(obj) {
                    controllerVar = obj;
                }

            };
        });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.