kudN008
kudN008

Reputation: 63

Angular JS Parent Scope Click Handler Not Destroyed

This is my html

<div controller="parent">
<button ng-click="doSomething()"><Click</button>
<div ng-view></div>
</div>

Here is my js controller for a particular view:

function child($scope) {
   $scope.$parent.doSomething = function() {
      // do http request or seomthing else
   }
}

Now when Im on the child controller view, angular does http request when i click on the button on the parent scope but when I navigate to other views with different controllers (of course), angular still does http request when I click on the button on the parent scope.

I thought when the view changes, the controller and including its scope will be destroyed and thereby effectively removing references to that particular scope objects.

My work around is to listen for $scope.on('$destroy') and override the function to return empty or something else.

Is there a better way to do this?

Thanks

Upvotes: 2

Views: 328

Answers (1)

GMStevens
GMStevens

Reputation: 179

$scope.$parent.doSomething is adding the doSomething property to that parent $scope object. So long as that parent scope doesn't get destroyed, your click handler will stick around.

Upvotes: 0

Related Questions