1

I have a method in a service which broadcasts an event and one controller has subscribed for it.

When broadcast happens the subscriber handler executes two times. Below is the setup:

//Broadcaster

function (Id1, Id2, Id3) {
    var requestObj = {"ID1": Id1, "ID3": Id3};
    $http.post(url, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    })
    .success(function (response) {
        $rootScope.$broadcast('myevent');
    }).error(function (response) {
    });
}
================================================================

//subscriber

$scope.$on('myevent', function () {
    console.log($dialog); // Executes two times
});

I am not able to rectify the cause. Is there something else which can make the difference. Thanks.

3
  • 4
    This could very well be due to duplicate controllers loaded for your view. A common reason for duplicate controller is due to controller declared in the route in $routeProvider configuration and also in the view html using ng-controller. Commented Jul 16, 2014 at 12:36
  • @Chandermani: Thanks. Exactly this was the issue, Does is matter which one I use? because in the subscriber I was opening a dialog and both the executions were opening dialogs from different templates. Commented Jul 16, 2014 at 12:50
  • I don't think it matters. I would add it as answer, so that the question can be marked answered Commented Jul 16, 2014 at 15:46

1 Answer 1

3

As it turns out there was multiple declaration of controller which was causing duplicate $on to trigger.

One of the common reason this happens is due to controller declared in the $routeProvider configuration and also in the view html using ng-controller

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

1 Comment

That's exactly it. Easy fix.

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.