Skip to content

Commit 7838a8b

Browse files
committed
Add article about event.
1 parent 36b9948 commit 7838a8b

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
1. [双向绑定](https://github.com/tiw/angularjs-tutorial/blob/master/bind.markdown)
66
1. [修饰器](https://github.com/tiw/angularjs-tutorial/blob/master/decorator.markdown)
77
1. [在controller间共享数据](https://github.com/tiw/angularjs-tutorial/blob/master/sharing-data-between-controllers.markdown)
8+
1. [在controller间共享数据](https://github.com/tiw/angularjs-tutorial/blob/master/event.markdown)

event.markdown

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Event 事件
2+
3+
事件是解耦良器,angularjs提供了很方便的事件机构。 发送事件可以用
4+
5+
```js
6+
$scope.$emit('name', 'args');
7+
```
8+
9+
或者是
10+
11+
```js
12+
$scope.$broadcast('name', 'args');
13+
```
14+
15+
要了解两者的差别, 首先要了解angularjs controller的scope的定义。 这里就不叙述了,
16+
简单来说 angularjs controller的scope就是一个像是js的基于prototye的对象继承。
17+
18+
看看下面这个例子.
19+
20+
首先我们定义几个嵌套的controller。 (木有嵌套, 基本上也不会用事件, 当然事件也可
21+
有$rootscope发出, 但这个首先慎用, 其次也是下面的一个特例)
22+
23+
```html
24+
<div ng-controller="ParentCtrl">
25+
26+
<div ng-controller="SelfCtrl">
27+
28+
<a class="btn" ng-click="click()">click me</a>
29+
<div ng-controller="ChildCtrl"></div>
30+
31+
</div>
32+
33+
<div ng-controller="BroCtrl"></div>
34+
</div>
35+
```
36+
37+
这里我们有四个controller, 层级关系如下:
38+
39+
```
40+
ParentCtrl
41+
-> SelfCtrl (*)
42+
-> ChildCtrl
43+
-> BroCtrl
44+
```
45+
46+
所有的事件都是由 SelfCtrl 发出去的.
47+
48+
## broadcast
49+
50+
事件发送的方法:
51+
52+
```js
53+
$scope.$broadcast
54+
```
55+
值得注意的是发送的主语是 $scope, 因为所有的事件其实都是作用在scope上的。
56+
57+
broadcast 是从发送者向他的子scope发送时间的一个方法。 这里就是SelfCtrl发送,
58+
SelfCtrl 和 ChildCtrl 会接受到, *而ParentCtrl是不会收到的*
59+
60+
事件的接受只有一个方法
61+
62+
```js
63+
$scope.$on
64+
```
65+
66+
例子如下
67+
68+
```js
69+
angular.module('TestApp', ['ng'])
70+
.controller('ParentCtrl', function($scope) {
71+
$scope.$on('to-child', function(e, d) {
72+
console.log('关我毛事');
73+
});
74+
})
75+
.controller('SelfCtrl', function($scope) {
76+
$scope.click = function () {
77+
$scope.$broadcast('to-child', 'haha');
78+
$scope.$emit('to-parent', 'hehe');
79+
}
80+
})
81+
.controller('ChildCtrl', function($scope){
82+
$scope.$on('to-child', function(e, d) {
83+
console.log('I\' the child, I got it', d);
84+
});
85+
})
86+
.controller('BroCtrl', function($scope){
87+
$scope.$on('to-child', function(e, d) {
88+
console.log('关我毛事');
89+
});
90+
})
91+
;
92+
```
93+
94+
点击clickme后, 在console里是看不到“关我毛事“的, 原因嘛就是 “管他毛事啊”
95+
96+
97+
## Emit
98+
99+
了解了broadcast之后, emit可以用以此类推解释了。
100+
101+
102+
```js
103+
angular.module('TestApp', ['ng'])
104+
.controller('ParentCtrl', function($scope) {
105+
$scope.$on('to-parent', function(e, d) {
106+
console.log('we are the parent, I got it', d);
107+
});
108+
})
109+
.controller('SelfCtrl', function($scope) {
110+
$scope.click = function () {
111+
$scope.$broadcast('to-child', 'haha');
112+
$scope.$emit('to-parent', 'hehe');
113+
}
114+
})
115+
.controller('ChildCtrl', function($scope){
116+
$scope.$on('to-parent', function(e, d) {
117+
console.log('关我毛事');
118+
});
119+
})
120+
.controller('BroCtrl', function($scope){
121+
$scope.$on('to-parent', function(e, d) {
122+
console.log('关我毛事');
123+
});
124+
})
125+
;
126+
```
127+
128+
## Notes
129+
130+
上面的例子可以看到, 事件和事件发生者的兄弟是没有关系的, 怎样都收不到。
131+
132+
133+
上面就是简单的事件介绍, 至于怎样组织事件传播、怎样拿到事件参数, 下回再说 ;)
134+
135+
136+
137+
## Demo
138+
139+
下面是live的例子, 好好玩吧
140+
141+
[example](http://plnkr.co/JFuE4S)

0 commit comments

Comments
 (0)