0

I have a selection of controllers:

Controller1
Controller2
Controller3

I also have a service which all of my Controllers call. At the moment at the top of every Controller ive created a scope declaring the Controller name:

$scope.ControllerName = 'Page1Controller';

I then pass this scope into the service:

$scope.$parent.Description = serviceMydata.getDescription($scope.ControllerName).toString();

Is there a way to not have to declare $scope.ControllerName?

My service:

app.service('serviceMydata', function(){

    return {
        getDescription: function(ControllerName) {
            return [
                mapData[ControllerName]
            ];
        }
    }
});

var mapData = {
    Page1Controller : 'Data to insert when on Page1',
    Page2Controller : 'Data to insert when on Page2',
    Page3Controller : 'Data to insert when on Page3',
    Page4Controller : 'Data to insert when on Page4',
}

2 Answers 2

1

You could do something like this when you declare your controllers:

(function(){

    var ctrlName = 'MainCtrl'

    app.controller(ctrlName, function($scope, service) {
        myService.fn(ctrlName);
    });

})();

Afaik, there isn't a simple way of doing it through angular and I don't see a reason to try.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. I've added some more of my service, are you able to see the purpose of it? Essentially to return a set of values based on the Controller
1

You could capture the name of your controller inside a closure in the service:

var app = angular.module('myapp', []);
app.service('myservicefactory', function () {
    return {
        create: function (name) {
            var controllerName = name;
            return {
                getDescription: function () {
                    console.log(controllerName);
                }
            };
        }
    };
});

app.controller('myctrl', ['$scope', 'myservicefactory', function ($scope, myservicefactory) {
    $scope.service = myservicefactory.create('myctrl');
    $scope.callService = function () {
        $scope.service.getDescription();
    };
}]);
app.controller('myctrl2', ['$scope', 'myservicefactory', function ($scope, myservicefactory) {
    $scope.service = myservicefactory.create('myctrl2');
    $scope.callService = function () {
        $scope.service.getDescription();
    };
}]);

Yes, you would have to instantiate your service and provide the controller name or whatever, but after that, it's captured inside your service. Here's a fiddle that demonstrates: http://jsfiddle.net/29wtqeyz/1/.

I don't think you want something more tightly coupled then that: keep in mind that your service should not be coupled to its consumer.

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.