B
B
Bogdan Bembenok2016-04-10 16:16:35
Angular
Bogdan Bembenok, 2016-04-10 16:16:35

How to make Angular remove all socket.io listener's only on controller reload?

Tell me, maybe there is some kind of event, like "controller reload" or something similar.

.controller('chatNotificationCtrl', ['$scope', 'socket', 'toastr', '$rootScope', '$state', function($scope, socket, toastr, $rootScope, $state) {
  alert('REFRESHED')
  $scope.chatNotifications = {};
  console.log('STARTED NOTIFICATION CONTROLLER');
  if ($scope.logged) {
    $scope.chatNotifications = $scope.logged.chatNotify;
  } else {
    return;
  }
  socket.on('notify:chat', function(data) {
    console.log('NOTIFY CHAT');
    $scope.chatNotifications.unshift(data);
    toastr.success('Example', 'New Message');
  });

  $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
    socket.getSocket().removeAllListeners();
  })

}])

I have done so for now, but if the state changes, this does not mean that the controller always restarts, there are nested routes just ..

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question