Understanding $rootScope event with $broadcast and $on

Pals,

Please refer my previous article for better explanation. Now directly i am moving to the execution of code.

Like how we used $scope just replace with $rootScope. Thats it. For reference here is the code..

index.html:

<html>
<head>
<script src=”angular.min.js”></script>
</head>
<body>
<script src=”script.js”></script>
<div ng-app=”sathya”>
<div ng-controller=”ParentCtrl”>
<button ng-click=”go()”>Button </button>
<div ng-controller=”SiblingCtrl”>
</div>
</div>
</div>
</body>
</html>

script.js:

var app = angular.module(‘sathya’,[]);

app.controller(‘ParentCtrl’,
function ParentCtrl ($scope,$rootScope) {
$scope.go = function() {
$rootScope.$broadcast(‘Sathyalog’,”How are you”)

}
});

app.controller(‘SiblingCtrl’,
function SiblingCtrl ($scope,$rootScope) {
$rootScope.$on(‘Sathyalog’, function (event, data) {
alert(“Iam executed”);
console.log(data);
});

});

Advertisements

One thought on “Understanding $rootScope event with $broadcast and $on

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s