I have a controller like this:
myApp.controller('loginCtrl', ['$scope', '$rootScope', '$http', '$location', function($scope, $rootScope, $http, $location){
console.log('check 1');
// before route update
//---------------------------------------------------------
$rootScope.$on('$locationChangeStart', function(event){
if($location.path() == "/login"){
console.log('check 2');
}
});
}]);
First time check 1 will be logged and When i click on /login link, check 2 will be logged. everytime i change path to /home and /login, i expect a new check 2 on console but what i see in console is like this:
check 2 (1 time)
check 2 (2 times)
check 2 (3 times)
check 2 (4 times)
...
this means by every location change, angularjs creates a new listener and number of check 2's increases!
Is there a standard way for listening on location change without this issue? or should i remove my listener before leaving /login page?
OTHER FILES
my app.js file:
...
when('/login', {
templateUrl: 'page/login.html',
controller: 'loginCtrl',
reloadOnSearch: false
})
...
login template:
<section class="login-page section">
<h3>Login</h3>
...
</section>