9951 explained code solutions for 126 technologies


angularjsHow do I use the AngularJS broadcast feature?


The $broadcast feature of AngularJS allows you to emit an event from a parent scope to all of its child scopes. This is useful for passing data between scopes.

Here is an example of how to use it:

// Parent controller
$scope.$broadcast('MyEvent', {
  someData: 'data to pass to child scope'
});

// Child controller
$scope.$on('MyEvent', function(event, args) {
  console.log(args.someData); // 'data to pass to child scope'
});

The first argument of the $broadcast function is the name of the event, and the second argument is the data to pass. This data will be passed to the $on function as its second argument.

In the example above, the parent scope broadcasts an event called MyEvent with some data. The child scope then listens for this event using the $on function, and logs the data to the console.

Here are some ## Helpful links

Edit this code on GitHub