0

Hello I have the following situation

I have a controller with a Method, that sets a timeout and calls a modal along with other actions:

angular
    .module('app')
    .controller('GlobalController', GlobalController);

function GlobalController($scope, $rootScope, ....) {
  var vm = this;

      $scope.$on("callMethod", function(){ 
         vm.showModal();
      });
}

I want to be able to access to that specific showModal() method from any other controller in the application

So I have the following options

$emit(name, args);

Dispatches an event name upwards through the scope hierarchy notifying the registered $rootScope.Scope listeners.

Using $emit I have to define the GlobalController as parent since its "upwards"

And:

$broadcast(name, args);

Dispatches an event name downwards to all child scopes (and their children) notifying the registered $rootScope.Scope listeners.

For any of those solutions i have to explicitly define the controller within the application (i mean on the view) in order to use it be as a parent or child, is there any way to do this, (even if it's not a controller)?

DJ22T
  • 1,628
  • 3
  • 34
  • 66
  • Turn your controller into a service. Inject that service into any controller that needs to call showModal. Don't use $broadcast and $emit as a way to perform RPC between controllers. – Reactgular Oct 15 '15 at 14:37
  • I can't use scope in a service, and how do i use the [Modal](https://angular-ui.github.io/bootstrap/#/modal) controller within a service? – DJ22T Oct 15 '15 at 15:10

0 Answers0