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.
myServiceto 10 controllers and each of them will havedoSomething? 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