0

I have $broadcast event its working from parent controller but i don't see object coming in child controller i am not sure what i am implementing wrong.

How can i achieve this task using angularJs event ?

parentCtrl.js

$scope.$broadcast('assessmentData',$scope.RiskAssessDTO);

childCtrl.js

if ($stateParams.assessmentId) {
    $scope.$on('assessmentData', function(s, assessmentData) {
        var assessmentData = assessmentData;
        console.log('assessmentData', assessmentData);
    });
}
2
  • Is the child loaded and is the if condition true BEFORE you attempt to broadcast? Commented May 3, 2016 at 15:27
  • yes child is loaded and if condition satisfy Commented May 3, 2016 at 15:31

1 Answer 1

2

If you want to use $broadcast you should use $rootScope.

$scope.startScanner = function() {

    $rootScope.$broadcast('scanner-started');
}

And then to receive, use the $scope of your controller:

$scope.$on('scanner-started', function(event, args) {

    // do what you want to do
});

https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$on

Regards,

Sign up to request clarification or add additional context in comments.

1 Comment

This isn't necessarily correct. You only NEED to use $rootScope if emitting alongside the root scope to deal with handlers registered at that level. In this case, broadcasting from the $rootScope may actually mask scoping issues where your scopes aren't at the levels you think they are at.

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.