0

I am trying to communicate between two controllers in my case

I have something like

html

<div ng-controller='testCtrl'>
    <div ng-click='clickBtn()'>clikc me</div>
</div>

..other contents...

<div ng-controller='testTwoCtrl'>
    other contents...
</div>

JS

  app.controller('testCtrl', function($scope) {
        $scope.clickBtn = function(){
            alert('hey')
        }
    }

    app.controller('testTwoCtrl', function($scope) {
        //is there anyway to know if clickBtn method is called from here?
    }

I was hoping to know if there is a way to notify testTwoCtrl if the button is clicked. Thanks for the help!

FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • It should work with [`$rootScope`](https://docs.angularjs.org/api/ng/service/$rootScope) if you need a quick and dirty solution. – PM 77-1 Jun 04 '14 at 21:44
  • If all you're doing is triggering an event in `testTwoCtrl`, then the link @Adjit supplied should steer you in the right direction. If you're looking to do more complex interactions between the two controllers, you should consider using a service as an intermediary component. – Marc Kline Jun 04 '14 at 21:54

1 Answers1

1

You can create a parameter for the clickBtn function.

I would suggest creating a boolean parameter and then if clickBtn is called from testTwoCtrl pass a value of true otherwise you can leave it blank in which case it will be false.

Adjit
  • 10,134
  • 12
  • 53
  • 98
  • I need to let testTwoCtrl knows that the element in testCtrl is being clicked. ng-click='clickBtn()' only exists in testCtrl scope but not testTwoCtrl scope. I don't believe your way would work. +1 though – FlyingCat Jun 04 '14 at 21:41
  • ooh, got it. Was a bit confused by your comment in the code. Well in that case take a look at this q/a : http://stackoverflow.com/a/19498009/1887101 – Adjit Jun 04 '14 at 21:46