Having a situation where a route (for example, /items
) and its controller has a method:
$scope.pathForItem = function (item) {...}
This method is used in the template to display the path for each item in a list. However, when I click on a link to change the route while on this page, Angular calls $rootScope.$apply()
somewhere in its code. This causes the re-evaluation of functions defined on the previous route's controller, including calling pathForItem
again for every item on the page.
Most of the time, this unnecessary re-evaluation doesn't cause any errors. But it becomes problematic when attempting to sign out. The pathForItem
function relies on the existence of a user
object (obtained from authService
). Normally, we ensure the user exists in this route's resolve
function. However, when signing out, Angular triggers $rootScope.$apply()
and attempts to call pathForItem
again for all items. When the user is removed mid-process and authService.currentUser()
starts returning null
, subsequent calls to pathForItem
result in errors...
Is it intended behavior for Angular's $rootScope.$apply
to invoke functions for the controller of the previous route? Is there a universal solution or must each controller's functions double-check if required objects exist, even after validation in the resolve
function?
This scenario is encountered using Angular 1.3.10 with ngRoute.