Forked from anonymous/gist:cd9762a69dd744d6093bd826c42d6eb1
Created
December 23, 2016 11:37
-
-
Save sivaprabug/00021c0f2e84572126e9e3324588c59e to your computer and use it in GitHub Desktop.
Broadcast concepts in AngularJS
This file contains 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> | |
<title>Broadcasting</title> | |
<script src="js/angular.min.js"></script> | |
<script> | |
var app = angular.module('app', []); | |
app.controller("firstCtrl", function ($scope) { | |
$scope.handleClick = function (msg) { | |
$scope.$broadcast('eventName', { message: msg }); | |
}; | |
}); | |
app.controller("secondCtrl", function ($scope) { | |
$scope.$on('eventName', function (event, args) { | |
$scope.message = args.message; | |
console.log($scope.message); | |
}); | |
}); | |
</script> | |
</head> | |
<body ng-app="app"> | |
<div ng-controller="firstCtrl" style="border:2px solid #E75D5C; padding:5px;"> | |
<h1>Parent Controller</h1> | |
<input ng-model="msg"> | |
<button ng-click="handleClick(msg);">Broadcast</button> | |
<br /><br /> | |
<div ng-controller="secondCtrl" style="border:2px solid #428bca;padding:5px;"> | |
<h1>Child Controller</h1> | |
<p>Broadcast Message : {{message}}</p> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment