<!DOCTYPE html>
<html ng-app="plnkrApp">
<head>
<script data-require="angular.js@1.3.16" data-semver="1.3.16" src="https://code.angularjs.org/1.3.16/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<meta charset="UTF-8" />
<script src="script.js"></script>
</head>
<body>
<pre>
Tree of scopes:
1 ($rootScope)
└ 2 ($scope of ParentCtrl)
└ 3 ($scope of ChildCtrl)
└ 4 ($scope of GrandChildCtrl)
</pre> {{ $id }}
<div ng-controller="ParentCtrl" style="border: 1px solid red">
{{$id}}
<br>
<div ng-controller="ChildCtrl" style="border: 1px solid green">
{{$id}}
<br>
<div ng-controller="GrandChildCtrl" style="border: 1px solid blue">
{{$id}}
<br>
<button ng-click="emitEvent()">Emit !</button>
</div>
<pre ng-show="sync$id">
event.currentScope.$id is : {{sync$id}}
Few millis later, event.currentScope.$id is : {{async$id||'undefined'}}</pre>
</div>
<pre ng-show="sync$id">
event.currentScope.$id is : {{sync$id}}
Few millis later, event.currentScope.$id is : {{async$id||'undefined'}}</pre>
</div>
</body>
</html>
angular.module('plnkrApp', [])
.controller('ParentCtrl', function($scope, $timeout) {
$scope.$on('customEvent', function(event) {
console.log(event.currentScope); // 003 the good one
$scope.sync$id = event.currentScope.$id;
$timeout(function() {
console.log(event.currentScope) // 002 the $rootScope
$scope.async$id = event.currentScope.$id;
});
})
})
.controller('ChildCtrl', function($scope, $timeout) {
$scope.$on('customEvent', function(event) {
console.log(event.currentScope); // 004 the good one
$scope.sync$id = event.currentScope.$id;
$timeout(function() {
console.log(event.currentScope) // 002 the $rootScope
$scope.async$id = event.currentScope.$id;
});
})
})
.controller('GrandChildCtrl', function($scope) {
$scope.emitEvent = function() {
$scope.$emit('customEvent');
};
});
/* Styles go here */
div, li {
padding: 5px;
}