<!DOCTYPE html>
<
html
>
<
head
>
<
script
src
=
</
script
>
<
script
type
=
"text/javascript"
>
var app = angular.module('app', []);
app.controller('ParentController', function (
$rootScope, $scope) {
$scope.broadcastMessage = function () {
$rootScope.$broadcast('newEvent', {
message: $scope.parentMessage
});
};
});
app.controller('ChildController', function ($scope) {
$scope.$on('newEvent', function (event, args) {
$scope.message = args.message;
});
});
app.controller('ExternalController', function ($scope) {
$scope.$on('newEvent', function (event, args) {
$scope.message = args.message;
});
});
</
script
>
<
style
type
=
"text/css"
>
h1, h2,
code {
color: green;
}
p {
color: green;
display: inline-block;
}
div {
border-color: black;
border-style: solid;
padding: 10px;
}
</
style
>
</
head
>
<
body
ng-app
=
"app"
>
<
h1
>GeeksforGeeks</
h1
>
<
h3
>AngularJS $rootScope.$broadcast</
h3
>
<
div
ng-controller
=
"ParentController"
>
<
h1
>Parent Controller</
h1
>
<
input
ng-model
=
"parentMessage"
>
<
button
ng-click
=
"broadcastMessage()"
>
Broadcast Message
</
button
>
<
br
><
br
>
<
div
ng-controller
=
"ChildController"
>
<
h2
>Child Controller</
h2
>
<
p
>Message :</
p
>
<
code
>{{message}}</
code
>
</
div
>
</
div
><
br
><
br
>
<
div
ng-controller
=
"ExternalController"
>
<
h1
>External Controller</
h1
>
<
p
>Message :</
p
>
<
code
>{{message}}</
code
>
</
div
>
</
body
>
</
html
>