0

How to get some specific controller's $scope variable?

    myApp.factory('Comment', ['$http', function ($http) {
        var comment = {};

        comment.add = function (shortName, taskNum, message) {
            // how to call controller's method
            // the following line dosen't work
            myApp.ListPanelController.showTaskDetail(shortName, taskNum);

        }

        return comment;
    }



    myApp.controller('ListPanelController', ['$scope', 'Comment',
        function ($scope, Comment) {
            $scope.showTaskDetail = function (shortName, taskNum) {
                Tasks.get(shortName, taskNum).success(function (data) {
                    // do something
                });
            }
        }]
    );      
1
  • You need to explain your scenario. Services are shared code\functionality and controllers are view specific. There should be no reason to call into a controller functionality from service. Commented Nov 23, 2013 at 13:38

1 Answer 1

1

You can't inject $scope into services, there is nothing like a singleton $scope.

But you can pass variables to your service or use $rootScope if you interesting to use $apply.

 myApp.factory('Comment', ['$http', '$rootScope', function ($rootScope,$http){/*..*/} 

But I would return promise and call showTaskDetail into controller.

myApp.factory('Comment', ['$http', '$rootScope', function ($rootScope,$http){/*..*/} 
Sign up to request clarification or add additional context in comments.

2 Comments

I'v just got an answer. You're right. I have to pass $scope variable to my service. Thanks!
you can pass $rootScope

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.