0

I know that you can create binding with a directive but is it possible with a controller? Maybe this is stupidly simple but I couldn't figure out.

For example I would like to click on a text in x scope's view and execute a function in another scope.

function firstCtrl() {
    var vm = this;
    vm.data = 'First scope stuff!';
}

function secondCtrl() {
    var vm = this;
    vm.data = 'Second scope stuff!';
  }

  vm.clicked = function() {
    alert('firstScope is clicked!'); 
  };

Here is what I have: http://plnkr.co/edit/CpPKsEGPZWzufB2K1FK3

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mar
  • 1,526
  • 6
  • 21
  • 46

1 Answers1

-1

There are multiple ways how to communicate between controllers. One way is emitting an event on scope:

function FirstCtrl($scope) 
{
  $scope.$on('someEvent', function(event, args) {});
  // another controller or even directive
}

function SecondCtrl($scope) 
{
  $scope.$emit('someEvent', args);
}

Another way sharing a service.

Hope it helps. Also check this https://stackoverflow.com/a/9407953/4782034

Community
  • 1
  • 1
Crowlex
  • 101
  • 7