0

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.

Hacker
  • 7,798
  • 19
  • 84
  • 154

2 Answers2

1

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

Ved
  • 11,837
  • 5
  • 42
  • 60
  • Your feedly is not exactly the same, you are omitting `$location.path()` and doing everything in the same controller, which I think is an important part of the problem. – jjimenez Oct 07 '15 at 19:00
-2

I think you are using a wrong approach. If you only want to pass a parameter to the new controller, why not use a route parameter? See the example at the end of this documentation page

Note that in your code, you are registering the $on after you broadcast the message, so it is not going to work.

jjimenez
  • 853
  • 1
  • 7
  • 18
  • $on is working. But in $on i am trying to change scope variables, which is not working – Hacker Oct 07 '15 at 08:50
  • The $on is correctly registered. And I don't understand how routeparameters could fit the problem ? – Elo Oct 07 '15 at 09:14
  • Maybe I missunderstood the question, but as I understand, he wants to redirect the user to a new view, and pass a value to its controller. Is it right? A route parameter is in my opinion the best choice for this. – jjimenez Oct 07 '15 at 09:20
  • I still believe that the `$broadcast` is not a good solution to this case, even with the negative vote to my answer :) – jjimenez Oct 07 '15 at 09:31