angular的父子controller通信

angularjs的事件 $broadcast and $emit and $on


Event 事件

事件是解耦良器,angularjs提供了很方便的事件机构。 发送事件可以用

  $scope.$emit('name', 'args');

或者是

  $scope.$broadcast('name', 'args');

要了解两者的差别, 首先要了解angularjs controller的scope的定义。 这里就不叙述了, 简单来说 angularjs controller的scope就是一个像是js的基于prototye的对象继承。

看看下面这个例子.

首先我们定义几个嵌套的controller。 (木有嵌套, 基本上也不会用事件, 当然事件也可 有$rootscope发出, 但这个首先慎用, 其次也是下面的一个特例)

<div ng-controller="ParentCtrl">

   <div ng-controller="SelfCtrl">
   
     <a class="btn" ng-click="click()">click me</a>
     <div ng-controller="ChildCtrl"></div>

   </div>
   
   <div ng-controller="BroCtrl"></div>
</div>

这里我们有四个controller, 层级关系如下:

 ParentCtrl
     -> SelfCtrl (*)
         -> ChildCtrl
     -> BroCtrl

所有的事件都是由 SelfCtrl 发出去的.

broadcast

事件发送的方法:

$scope.$broadcast

值得注意的是发送的主语是 $scope, 因为所有的事件其实都是作用在scope上的。

broadcast 是从发送者向他的子scope发送时间的一个方法。 这里就是SelfCtrl发送, SelfCtrl 和 ChildCtrl 会接受到, 而ParentCtrl是不会收到的

事件的接受只有一个方法

$scope.$on

例子如下

angular.module('TestApp', ['ng'])
.controller('ParentCtrl', function($scope) {
 $scope.$on('to-child', function(e, d) {
   console.log('关我毛事');
 });
})
.controller('SelfCtrl', function($scope) {
  $scope.click = function () {
    $scope.$broadcast('to-child', 'haha');
    $scope.$emit('to-parent', 'hehe');
  }
})
.controller('ChildCtrl', function($scope){
  $scope.$on('to-child', function(e, d) {
    console.log('I\' the child, I got it', d);
  });
})
.controller('BroCtrl', function($scope){
  $scope.$on('to-child', function(e, d) {
    console.log('关我毛事');
  });
})
;

点击clickme后, 在console里是看不到“关我毛事“的, 原因嘛就是 “管他毛事啊”

Emit

了解了broadcast之后, emit可以用以此类推解释了。

angular.module('TestApp', ['ng'])
.controller('ParentCtrl', function($scope) {
 $scope.$on('to-parent', function(e, d) {
    console.log('we are the parent, I got it', d);
 });
})
.controller('SelfCtrl', function($scope) {
  $scope.click = function () {
    $scope.$broadcast('to-child', 'haha');
    $scope.$emit('to-parent', 'hehe');
  }
})
.controller('ChildCtrl', function($scope){
  $scope.$on('to-parent', function(e, d) {
   console.log('关我毛事');
  });
})
.controller('BroCtrl', function($scope){
  $scope.$on('to-parent', function(e, d) {
    console.log('关我毛事');
  });
})
;

Notes

上面的例子可以看到, 事件和事件发生者的兄弟是没有关系的, 怎样都收不到。

上面就是简单的事件介绍, 至于怎样组织事件传播、怎样拿到事件参数, 下回再说 ;)

转载自:https://github.com/tiw/angularjs-tutorial/blob/master/event.markdown
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值