Created
March 28, 2014 14:42
-
-
Save anonymous/9834438 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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