Shashank Vivek
Shashank Vivek

Reputation: 17514

Changing $scope from inside $rootScope is not getting reflected

I am trying to show one button as in this Plunker

<div ng-show="showbtn"><button class="fix btn btn-success" ng-click="top()">To the top</button></div>

On scroll event, I have made $rootScope.$emit call and it is getting triggered too, but not sure why the $scope value is not getting changed inside the mainCtrl controller $scope. Is $scope inside $rootScope is different ?

Upvotes: 0

Views: 629

Answers (1)

James Allardice
James Allardice

Reputation: 166041

The event handler (the function passed to $rootScope.$on) runs outside of Angular's normal digest cycle so you need to tell the parent scope that something has changed. You can use $apply to do so:

$rootScope.$on('scrolled',function(event,data){
  $scope.$apply(function () {
    $scope.showbtn = data.message;
  });
});

Here's an updated Plunker.

Upvotes: 2

Related Questions