Angularjs: How to prevent creating multiple event listener on controller refresh -


i have controller 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 logged , when click on /login link, check 2 logged. everytime change path /home , /login, expect new check 2 on console see in console this:

check 2 (1 time) check 2 (2 times) check 2 (3 times) check 2 (4 times) ... 

this means every location change, angularjs creates new listener , number of check 2's increases! there standard way listening on location change without issue? or should remove 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> 

this because subscribing $rootscope.$on in controller. when subscribe way, angularjs has no chance clean listeners when local $scope destroyed.

just subscribe $scope.$on('$locationchangestart', fn); if subscribe way angularjs automatically remove listener when local $scope destroyed.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -