Skip to content

Commit f939d17

Browse files
committed
Directive to directive communication
1 parent 52f7dea commit f939d17

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

directive2directive.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
5+
6+
<title>Directive to Directive</title>
7+
8+
<link rel="stylesheet" href="vendor/zurb/foundation/stylesheets/foundation.css" type="text/css" media="screen" charset="utf-8">
9+
10+
</head>
11+
12+
<body ng-app="superApp">
13+
14+
<superhero flight speed strength>Superman</superhero>
15+
<superhero speed>The Flash</superhero>
16+
<superhero strength>The Hulk</superhero>
17+
18+
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js"></script>
19+
<script src="directive2directive.js"></script>
20+
</body>
21+
</html>

directive2directive.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var app = angular.module('superApp', []);
2+
3+
app.directive('superhero', function() {
4+
return {
5+
restrict: 'E',
6+
scope: {},
7+
8+
controller: function($scope) {
9+
$scope.abilities = [];
10+
11+
this.addStrength = function() {
12+
$scope.abilities.push('strength');
13+
}
14+
15+
this.addSpeed = function() {
16+
$scope.abilities.push('speed');
17+
}
18+
19+
this.addFlight = function() {
20+
$scope.abilities.push('flight');
21+
}
22+
},
23+
24+
link: function(scope, element) {
25+
element.addClass('button');
26+
element.bind('mouseenter', function() {
27+
console.log(scope.abilities)
28+
});
29+
}
30+
}
31+
});
32+
33+
app.directive('strength', function() {
34+
return {
35+
require: "superhero",
36+
link: function(scope, element, attrs, superheroCtrl) {
37+
superheroCtrl.addStrength();
38+
}
39+
}
40+
})
41+
app.directive('speed', function() {
42+
return {
43+
require: "superhero",
44+
link: function(scope, element, attrs, superheroCtrl) {
45+
superheroCtrl.addSpeed();
46+
}
47+
}
48+
})
49+
app.directive('flight', function() {
50+
return {
51+
require: "superhero",
52+
link: function(scope, element, attrs, superheroCtrl) {
53+
superheroCtrl.addFlight();
54+
}
55+
}
56+
})

0 commit comments

Comments
 (0)