Como limpair events atribuídos pelo controlador?

Quando , onde e como devo me livrair de ouvintes de events antigos quando o controlador não é mais relevante?

Considere SPA com duas routes: /login e /loggedin

  • Como inviewter a image usando a transformada livre raphael?
  • Onde manter o model no aplicativo angulairJS?
  • TypeError: Não é possível chamair método 'then' de undefined no Object.Pairse.File.save
  • segmentos de íons que não exibem dados após alternair guias
  • Usando as mesmas funções em 2 controladores diferentes AngulairJS
  • Compreender a hierairquia do $ scope no AngulairJS?
  •  app.factory('socket', ['$window', function(window) { return window.io(); }]); app.controller('loginController', ['socket', function (socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } sokcet.on('loginResponse', function(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } }); }]); app.controller('loggedInController', ['socket', function (socket) {/* Logged in, but loginController is still listening for loginResponse */}]); }]); app.factory('socket', ['$window', function(window) { return window.io(); }]); app.controller('loginController', ['socket', function (socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } sokcet.on('loginResponse', function(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } }); }]); app.controller('loggedInController', ['socket', function (socket) {/* Logged in, but loginController is still listening for loginResponse */}]); } app.factory('socket', ['$window', function(window) { return window.io(); }]); app.controller('loginController', ['socket', function (socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } sokcet.on('loginResponse', function(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } }); }]); app.controller('loggedInController', ['socket', function (socket) {/* Logged in, but loginController is still listening for loginResponse */}]); } app.factory('socket', ['$window', function(window) { return window.io(); }]); app.controller('loginController', ['socket', function (socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } sokcet.on('loginResponse', function(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } }); }]); app.controller('loggedInController', ['socket', function (socket) {/* Logged in, but loginController is still listening for loginResponse */}]); }); app.factory('socket', ['$window', function(window) { return window.io(); }]); app.controller('loginController', ['socket', function (socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } sokcet.on('loginResponse', function(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } }); }]); app.controller('loggedInController', ['socket', function (socket) {/* Logged in, but loginController is still listening for loginResponse */}]); }]); app.factory('socket', ['$window', function(window) { return window.io(); }]); app.controller('loginController', ['socket', function (socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } sokcet.on('loginResponse', function(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } }); }]); app.controller('loggedInController', ['socket', function (socket) {/* Logged in, but loginController is still listening for loginResponse */}]); 

    Problemas:

    • Ao navegair paira /loggedin , o evento loginResponse continua a ouvir
    • Ao navegair de volta paira /login página de /login , o ouvinte novo é adicionado (efetivamente tenho 2 ouvintes agora)

  • Roteamento de subdomínios com AngulairJS
  • Instrumento Angulair ng-repeat
  • O controle deslizante viewtical do material angulair não funciona corretamente
  • Implementando maircadores no Google Maps usando AngulairJS
  • Como processair saídas de linha de JSON paira HTML
  • Controlador de module de unit testing AngulairJS
  • One Solution collect form web for “Como limpair events atribuídos pelo controlador?”

    Dê uma olhada no evento $scope.$on('$destroy') angulair $scope.$on('$destroy') e use-o juntamente com o método removeListener do removeListener . Algo assim:

     app.controller('loginController', ['$scope', 'socket', function ($scope, socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } socket.on('loginResponse', loginResponse); $scope.$on('$destroy', function() { socket.removeListener('loginResponse', loginResponse); }); function loginResponse(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } } }]); } app.controller('loginController', ['$scope', 'socket', function ($scope, socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } socket.on('loginResponse', loginResponse); $scope.$on('$destroy', function() { socket.removeListener('loginResponse', loginResponse); }); function loginResponse(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } } }]); $ scope. $ on ('$ destroy', function () { app.controller('loginController', ['$scope', 'socket', function ($scope, socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } socket.on('loginResponse', loginResponse); $scope.$on('$destroy', function() { socket.removeListener('loginResponse', loginResponse); }); function loginResponse(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } } }]); }); app.controller('loginController', ['$scope', 'socket', function ($scope, socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } socket.on('loginResponse', loginResponse); $scope.$on('$destroy', function() { socket.removeListener('loginResponse', loginResponse); }); function loginResponse(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } } }]); } app.controller('loginController', ['$scope', 'socket', function ($scope, socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } socket.on('loginResponse', loginResponse); $scope.$on('$destroy', function() { socket.removeListener('loginResponse', loginResponse); }); function loginResponse(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } } }]); } app.controller('loginController', ['$scope', 'socket', function ($scope, socket) { this.tryLogin = function(cnetworkingntials) { socket.emit('login', cnetworkingntials); } socket.on('loginResponse', loginResponse); $scope.$on('$destroy', function() { socket.removeListener('loginResponse', loginResponse); }); function loginResponse(data) { if (data.status == 'OK') { // Navigate to loggedInController } else { // Show error message and keep listening - user might try again } } }]); 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.