Skip to content

Instantly share code, notes, and snippets.

@duycuong87vn
Last active August 29, 2015 13:57
Show Gist options
  • Save duycuong87vn/9834079 to your computer and use it in GitHub Desktop.
Save duycuong87vn/9834079 to your computer and use it in GitHub Desktop.
function EventController($scope){
$scope.count = 0;
$scope.$on('MyEvent', function(){
$scope.count++;
});
}
<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<meta charset="utf-8">
<title>AngularJS review</title>
</head>
<body>
<div ng-controller="EventController">
Root scope <tt>My event</tt> count : {{count}}
<ul>
<li ng-repeat="item in [1]" ng-controller="EventController">
<button ng-click="$emit('MyEvent')">
$emit('MyEvent')
</button>
<button ng-click="$broadcast('MyEvent')">
$broadcast('MyEvent')
</button>
<br>
<ul>
<li ng-repeat="i in [0, 1, 2]" ng-controller="EventController">
Leaf scope <tt>MyEvent</tt> count: {{count}}
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment