uday s uday s - 2 years ago 106
Javascript Question

$emit not working while emit data from one controller to another controller in angularjs

I try to call function while emit data from one controller to another
in AngularJS. But it doesn't work for me in Angular v.1.2. I need to call

function from
. Anyone can give me some idea for this issue?

Sample code:

<div ng-contorller="ctrl">
<div ng-contorller="ctrl1">
<div ng-contorller="ctrl2">

function parentctrl(){

function ctrl1($scope){
$scope.callfn = function(){
$scope.$on('emitdata', function(event, data){
function ctrl2($scope){
$scope.$emit('emitdata', {'key':'uu'});

Answer Source

Or you could use both emits and broadcasts. Child scopes emits, parents broadcast. Like so: http://plnkr.co/edit/pjGIHByzkwCeNiaVrdb6?p=preview

app.controller("ctrl", function($scope) {
  $scope.$on('emitdata', function(event, data) {
    // This event will reach: ctrl -> ctrl1 + ctrl2
    $scope.$broadcast('broadcast-data', data);

app.controller("ctrl1", function($scope) {
  $scope.$on('broadcast-data', function(event, data) {
    $scope.received = data;

app.controller("ctrl2", function($scope) {
  $scope.emit = function() {
    // This event will reach: ctrl2 -> ctrl -> rootScope
    $scope.$emit('emitdata', {'key': 'uu'}); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download