Skip to content

Instantly share code, notes, and snippets.

@ysyun
Created April 14, 2013 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysyun/5382212 to your computer and use it in GitHub Desktop.
Save ysyun/5382212 to your computer and use it in GitHub Desktop.
AngularJS Directive Sample. Directive to Directive Communication
/*<div ng-app="superApp">
<superhero flight speed strength>Superman</superhero>
<superhero speed>Flash</superhero>
<superhero strength>Hulk</superhero>
</div>*/
var app = angular.module("superApp", []);
app.directive("superhero", function() {
return {
restrict: "E",
scope: {},
controller: function($scope) {
$scope.abilities = [];
this.addStrength = function() {
$scope.abilities.push("strength")
}
this.addSpeed = function() {
$scope.abilities.push("speed")
}
this.addFlight = function() {
$scope.abilities.push("flight")
}
},
link: function(scope, element) {
element.addClass("button");
element.bind("mouseenter", function() {
console.log(scope.abilities);
})
}
}
});
app.directive("strength", function() {
return {
require: "superhero",
link: function(scope, element, attrs, superheroCtrl) {
superheroCtrl.addStrength();
}
}
}) ;
app.directive("speed", function() {
return {
require: "superhero",
link: function(scope, element, attrs, superheroCtrl) {
superheroCtrl.addSpeed();
}
}
}) ;
app.directive("flight", function() {
return {
require: "superhero",
link: function(scope, element, attrs, superheroCtrl) {
superheroCtrl.addFlight();
}
}
}) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment