1

in ControllerOne

$scope.displayReport = function( status ){

   statusCkeck( status ) ;
}

statusCkeck() ; is in another controller -controllerTwo

How can i call this function from one controller to another

i tried angular.element(document.getElementById('controllerTwo')).scope().statusCheck();

But it throwing error.

Please suggest

3
  • But my case is different ,i just want to call another function ,no service calls Commented Sep 28, 2013 at 11:58
  • 2
    Please don't do this... Commented Sep 28, 2013 at 12:04
  • 1
    That is not angular way. Use a service if you want to share things between controllers. Commented Sep 28, 2013 at 12:06

2 Answers 2

7

You should use a service instead to avoid muddying up the controllers with code that calls functions in other controllers. If you're sharing functions, just define them in a service and inject that service where needed.

angular.module('myApp', [])
  .factory('myService', function() {
    return {
      sharedFunction: function (foo, bar) {
        return foo + bar;
      }
    }
};
Sign up to request clarification or add additional context in comments.

Comments

1

You can get scope:

angular.element(document.getElementById('yourControllerElementID')).scope().get()

But yourControllerElementID should be ElementID not ControlerName!

For example:

<div id="elemID" ng-controller="YourController"></div>

So elemId is id of element with YourController.

4 Comments

Here i confused ,what is Element ID in the controller
See edit. Just take Id of element where you using your controller.
This should work but it's a terrible idea. Tightly coupling your controllers like that leads to spaghetti code.
Yes this is not good solution. Maybe he can move method to service and use it globally :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.