Skip to content

Instantly share code, notes, and snippets.

@slattery
Forked from anonymous/jsbin.ozuyus.html
Created March 28, 2014 14:43
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 slattery/9834448 to your computer and use it in GitHub Desktop.
Save slattery/9834448 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body ng-app>
<div ng-controller="ParentCtrl">
<p>{{foo}}</p>
<div ng-controller="ChildCtrl">
<button ng-click="onButtonClick()">{{buttonTitle}}</button>
</div>
</div>
</body>
</html>
function ParentCtrl($scope){
$scope.foo = "Hello";
//Emit to Parent example part 2
$scope.$on("UPDATE_PARENT", function(event, message){
$scope.foo = message;
//Broadcast to Child example part 1
$scope.$broadcast("DO_BIDDING", {
buttonTitle : "Taken over",
onButtonClick : function(){
$scope.foo = "HAHA this button no longer works!";
}
});
});
}
function ChildCtrl($scope){
$scope.buttonTitle = "Update Parent";
$scope.onButtonClick = function(){
//Emit to Parent example part 1
this.$emit("UPDATE_PARENT", "Updated");
};
//Broadcast to Child example part 2
$scope.$on("DO_BIDDING", function(event, data){
for(var i in data){
$scope[i] = data[i];
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment