在$ scope。$ on中使用当前控制器范围和父控制器范围

时间:2016-12-14 20:54:27

标签: javascript angularjs angularjs-scope broadcast

我正在广播到子范围(从一个父控制器到子控制器)。在$ scope。$ on

app.controller('myCtrl')

    $scope.$on('broadcastName', function(e, d){       
       //I want to be able to access myCtrl's scope here even though
       //the broadcast comes from another controller 
    });

我希望能够访问当前控制器范围,该范围用我的$ scope。$ on函数捕获广播,而不是发出广播的父控制器。

当我写这个问题时,我想出了答案。

1 个答案:

答案 0 :(得分:0)

我希望这能节省一些时间

 app.controller('myCtrl')

 $scope.$on('broadcastName', function(e, d){
   /* if you want the scope of the controller that made the broadcast */
   console.log('parent scope', e.targetScope);
   /* if you want the scope of the controller that caught the broadcast it is just your basic scope */
   console.log('child scope', $scope);
 });