Skip to content

Commit 1b611de

Browse files
committed
add 'directive_communication' branch. Children directives calling parent directives' controllers
1 parent 1c02020 commit 1b611de

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

egghead/index.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
<h1>Angular</h1>
1212

1313
<div ng-app="app">
14-
<div ng-controller="AppCtrl"></div>
14+
<country>
15+
<state>
16+
<city></city>
17+
</state>
18+
</country>
1519
</div>
1620

1721
<script src="js/angular.js"></script>

egghead/js/app.js

+31-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
// Define an App
22
var app = angular.module('app', []);
33

4-
app.controller('AppCtrl', function($scope, $q) {
5-
6-
var defer = $q.defer();
7-
8-
defer.promise
9-
.then(function(weapon) {
10-
alert('You can have my ' + weapon);
11-
12-
return 'bow';
13-
})
14-
.then(function(weapon) {
15-
alert('And my ' + weapon);
16-
17-
return 'axe';
18-
})
19-
.then(function(weapon) {
20-
alert('As well as my ' + weapon);
21-
})
4+
app.directive('country', function() {
5+
return {
6+
restrict: 'E',
7+
controller: function() {
8+
this.makeAnnouncement = function(message) {
9+
console.log('Country says: ' + message);
10+
}
11+
}
12+
};
13+
});
2214

23-
defer.resolve('sword');
15+
app.directive('state', function() {
16+
return {
17+
restrict: 'E',
18+
require: '^country',
19+
controller: function() {
20+
this.makeLaw = function(law) {
21+
console.log('Law: ' + law);
22+
}
23+
},
24+
link: function(scope, element, attrs, countryCtrl) {}
25+
};
26+
});
2427

28+
app.directive('city', function() {
29+
return {
30+
restrict: 'E',
31+
require: ['^country', '^state'],
32+
link: function(scope, element, attrs, ctrls) {
33+
ctrls[0].makeAnnouncement('from city');
34+
ctrls[1].makeLaw('Jump higher');
35+
}
36+
};
2537
});

0 commit comments

Comments
 (0)