在我的情况下,如何在两个控制器之间进行通信

How to communicate between two controller in my case?

本文关键字:控制器 两个 之间 通信 情况下 我的      更新时间:2023-09-26

我正在尝试在我的情况下两个控制器之间进行通信

我有一个类似

的东西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?
    }

我希望知道是否有一种方法来通知testTwoCtrl,如果按钮被点击。谢谢你的帮助!

可以为clickBtn函数创建参数

我建议创建一个布尔参数,然后如果clickBtn是从testTwoCtrl传递true的值调用,否则你可以让它留空,在这种情况下,它将是假的。