ps0604 ps0604 - 3 months ago 8x
AngularJS Question

Angular: $broadcast and $emit send message objects by value or reference?

Given the following code:

var msg = {
field1: val1,
field2: val2,
// more fields

$scope.$broadcast("EventName", msg);

The event consumer receives a pointer to
or a copy?


The event consumer receives a pointer to event data.

For example:

<div ng-controller="MyCtrl">
    <input type="text" ng-model=""/>
    <button ng-click="broadcast()">Broadcast event</button>
<div ng-controller="MyCtrl2">
    <input type="text" ng-model=""/>

function MyCtrl($scope,$rootScope) {
    $ = {name: "MyCtrl"};
    $scope.broadcast = function(){
        $rootScope.$broadcast('someEvent', $;

function MyCtrl2($scope,$rootScope) {
    $scope.name2 = null;
    $scope.$on('someEvent', function(event, data){
        $scope.name2 = data;

See this JSFiddle for a demonstration on that.

Just broadcast the value from the first input field using the button and then try to change the value of any input field.