0

This is my controller which is broadcasting the request on every button click ! but it is not firing any event !

app.controller("education-blog", function ($scope, $http, $state, $ionicPopup, blogfav,URL, educationB, $cordovaSocialSharing, $localStorage, $rootScope) {
    $scope.edu = educationB.getEdu($state.params.id);


    $scope.class = $scope.edu.is_favorite;
    $localStorage.blogscount = 0;

    $scope.favbutton = function (id) {
        if ($scope.class === true) {
            $scope.class = false;
        }
        else {
            $scope.class = true;
        }
        var data = {
            id: id,
            type: 'blogs'
        };
        $http.post(URL.url+'/favorites', data).success(function (response) {
            console.log(response.is_favorite);
            if (response.is_favorite == true) {
                $localStorage.blogscount++;
            }
            if (response.is_favorite == false) {
                $localStorage.blogscount--;
            }
        }).error(function () {
            $ionicPopup.alert({
                title: 'Something went wrong !',
                template: ' Sorry for the Inconvenience !'
            });
        });
        $rootScope.$broadcast('update', 9);
    }
});

this is my second controller which contain $scope.on()//

    app.controller('blogfav', function (blogfav, $scope) {

        $scope.blogfavourites = blogfav;

        $scope.$on('update', function (event, arg) {
            $scope.receiver = 'got your ' + arg;
            console.log($scope.receiver);
        });

what I want is when i click on my favbutton it must fire the event which must be catch by my second controller

These two are separate controllers

1 Answers1

0

I can only imagine that the two controllers do not share a $rootScope.

Perhaps they belong to different modules/apps, and are not 'visible' to each other.

$rootScope.$broadcast vs. $scope.$emit is a good explanation of how broadcast and emit work in angularjs.

Community
  • 1
  • 1
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
  • Actually, I am having a problem that when my controller which needs to catch is not open once (means it is not registered at least once!) it is not catching up the event else it is catching the events ! what should I do ? if my controller is not registered ? and i can still make it catch the request ? – Kartik Khandelwal Aug 17 '16 at 13:36
  • This answer is good, I just want to add that the 2 controllers might be defined in the same module, but the first one broadcasts on $rootScope and the second one listens on its personal $scope. It seems very unlikely that these 2 are the same. – Cristina_eGold Aug 17 '16 at 13:37
  • @deepdownunder2222 my understanding was that `$rootScope.broadcast` would go down to *all* scopes, which is picked up by the `on` or each scope. – AncientSwordRage Aug 17 '16 at 13:51
  • @KartikKhandelwal I'm struggling to understand what you've said. Are you saying that you have two modules? Can you [edit](http://stackoverflow.com/posts/38996710/edit) your post to show us where you define your module (`app = angular.module` or similar) and where the two controllers are 'used' (either `
    ` or in say your `routeProvider`)?
    – AncientSwordRage Aug 17 '16 at 13:53
  • @Pureferret, you are absolutely right. I made a short refresh and $rootScope.$broadcast indeed sends the message to both the listeners registering $rootScope.$on and the ones that have a child scope and register $scope.$on, since $broadcast's way of working is downstream (all children scopes are elligible to be notified), as opposed to $emit. – Cristina_eGold Aug 17 '16 at 14:22
  • @pureferret , actually my code is not working bcoz my second controller is not registered to handle the request ? what I found is that ! when I open the page of the second controller and then open the page of the first controller than when I do some operations then it works correctly ..but when I refresh my application and open my first page before opening the second page it won't work ! (my both controller are completely distinct) and on the different page ! – Kartik Khandelwal Aug 17 '16 at 17:01
  • (i need to ask one thing is -- do controller register themselves when we open the page ? ) or they register themselves in config file when app starts automatically – Kartik Khandelwal Aug 17 '16 at 17:01
  • @KartikKhandelwal page loads, ` – AncientSwordRage Aug 18 '16 at 10:52