0

Is there any possibility to call one controller function in another controller. I have been seeing that the data is manipulating in between controllers, but not the functions.

For Example. I have two controllers.

Module.Controller('Controller1', function($scope){ $scope.function1 = function(){}; }); Module.Controller("Controller2", function(){ // I need to call the function function1() from the controller1 }); 

Is this possible? Can you help me to solve this.

2 Answers 2

3

If you need to reuse logic from another controller, I would suggest moving that logic to a service that you can inject in both controllers. This is a much cleaner solution and is easier testable.

Example:

Module.service("Service1", function(){ this.function1 = function(){ ... } }); Module.Controller('Controller1', function($scope, Service1){ $scope.function1 = Service1.function1; }); Module.Controller("Controller2", function(Service1){ // I need to call the function function1() from the controller1 // simply call Service1.function1 here }); 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, we can use services/factories/providers in order to achieve this kind of behavior.I tried that too before ask the question, it worked fine for me. But i don't want to be work with those and also i'm not interest to use $rootscope too. Apart from these any solution will be appreciable.
@Bhimisetty is there a specific reason you do not want to make use of services? I understand your objection against using $rootScope, that's a solution I wouldn't suggest either. You should indeed keep the $rootScope clean.
Thank you for your feedback. In my application i am handling a lot of functions in the controllers. Some of the functions are common for 2 or more controllers. In fact, if i use the services, again it is a burden for my application i think. In my point of view, if we are working with data then only we go for services, in my point, am not using data. Am using modal popups only. If no solution is found, then eventually i need to adopt the services.
Hey @Bhimisetty, thanks for your response. I'm a bit confused by your objection towards using a service in Angular. They really don't encumber your application in any way.
0

You have cannot call one function into another controller directly. But, you can change the thing in the following ways:

  1. Use $rootScope.function1 instead of $scope, $rootScope is available from every $scope
  2. Use $rootScope.$emit to call an event from Controller2 and catch it with $rootScope.$on in Controller1 and then use the function
  3. You can define the function in a Service and use it from both controllers
  4. You can use a $scope.$watch or $rootScope.$watch if the controller 2 can change some variable in the Controller1 scope, or use $rootScope.$watch unless

1 Comment

1. I don't want to be use $rootscope 2. I think this approach is not the required one for my question, this will be used in calling one function which executes after an action in another controller. 3. Through services, i have achieved. But i don't want this too. 4. This approach is also not suitable for my requirement. My requirement is just call the function by injecting the another controller. Thank you for your feedback

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.