angularjsangular-broadcast

Broadcast and on not working as desired


I am trying to broad cast from 1 controller and watch for that variable on another controller and expecting some changes on the second controller. But its not working.

Below is the first controller code

$scope.gotoKBPage = function()
      {
         $location.path('/kb');
         $rootScope.$broadcast('newVar',{id: "123"});
      };

Below is the second controller code which is the /kb page

  $scope.newPage = function()
  {
      $scope.pernotes     = true; // this makes a section visible, but not happening
  };

   $rootScope.$on('newVar', function(event, data)
   {
    if(data)
    {
        $scope.newPage();
    }
   });

Is there anything i need to do explicitly for this to take effect.


Solution

  • Try this:

     $scope.newPage = function()
      {  
           $scope.$apply();
           $scope.pernotes     = true; // this makes a section visible, but not happening
      }
    

    EDIT: i have tested. It should work without $apply too. Check this link:fiddle