0

Please help me in my question. Why doesn't work function $broadcast when I call from parent controller and catch in the child controller?

<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
    <script>
        var app = angular.module('app', []);
        app.controller('ctrl1',
                function ($scope) {
                    $scope.$broadcast('parent', 'Some data');
                });

        app.controller('ctrl2',
                function ($scope) {
                    $scope.$on('parent', function (event, data) {
                        console.log(data); // 'Some data'
                    });
                });
    </script>
</head>
<body>
<div ng-controller="ctrl1">
    <div ng-controller="ctrl2">
    </div>
</div>
</body>
</html>

3 Answers 3

4
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
    <script>
        var app = angular.module('app', []);
        app.controller('ctrl1',
                function ($scope) {
                    $scope.$on("ready",function(){
                      $scope.$broadcast('parent', 'Some data');
                    });

                });

        app.controller('ctrl2',
                function ($scope) {
                    $scope.$on('parent', function (event, data) {
                       alert(data); // 'Some data'
                    });
                    $scope.$emit('ready');
                });
    </script>
</head>
<body>
<div ng-controller="ctrl1">
    <div ng-controller="ctrl2">
    </div>
</div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

6 Comments

It was very useful for me. Thx!
One question. Why emit must be on last line? And if move $emit upper than $scope.$on it is not works.
@DK3 basically you are doing an emit first when the child controller is ready then listening it from parent and then using it to broadcast again. This is absolutely not practical and bad practice. this cause two propagations. which is unnecessary.
@Maksim this method is working but this should not be considered for using in real applications because two propagations for a single method to work is unnecessary
if you do so Maksim then again it you were just broadcasting your message for parent when the listener was not ready for child scope
|
1

Change $scope.$broadcast('parent', 'Some data');

to

 $scope.$broadcast('parent', {message:'Some data'});

1 Comment

No It is not necessary to send it as an object. see this, Its just the child is not ready for listening.
1

I've made a fiddle from your code. You were just broadcasting your message when the listener was not ready.

Just broadcast your message when the controller2 is ready to listen.

var app = angular.module('app', []);
app.controller('ctrl1',
  function($scope, $timeout) {
    $timeout(function() {
      $scope.$broadcast('parent', 'Some data');
    }, 0)
  });

app.controller('ctrl2',
  function($scope) {
    $scope.$on('parent', function(event, data) {
      console.log(event, data); // 'Some data'
    });
  });

Hope this helps

5 Comments

Do you have any alternative variants instead timeout?
Well $timeout is not necessary here but to wait for the ctrl2 to be defined and come in its listening mode. Basically ctrl2 is inside ctrl1 and ctrl1 needs to be defined properly for the view inside it. now when the code inside ctrl1 initiates the listener is not loaded and is not in its state
In the emit case timeout is not used fiddle
emit propagates it to the parent controllers and broadcast do this for their child. emit is working because the listener on top of it is ready.
@Maksim See this to understand. its just that the broadcast is fired before the child is ready to listen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.