Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doesn't remove or empty array on listener after change route #154

Open
essivision opened this issue Feb 26, 2017 · 4 comments
Open

doesn't remove or empty array on listener after change route #154

essivision opened this issue Feb 26, 2017 · 4 comments

Comments

@essivision
Copy link

Hi,
I defined a service like this:

'use strict';

app.factory('mySocket', function(socketFactory) {
    return socketFactory({
        ioSocket: io.connect(':3001',{forceNew: true})
    });
});

and in controller:

app.controller('ChatCtrl',function($scope, mySocket,$rootScope) {
    $scope.arr = []; 
    $scope.arr.length = 0; 
    $scope.currentChannel = 1;

 Restangular.all('chat/message/').getList().then(function (data) {
            $scope.arr[$scope.currentChannel] =data;

  mySocket.on('connect', function () {
        console.log('CONNECT');

        mySocket.on('messages.new', function (data) {
             var tmp = data;
                   tmp.timestamp = data.timestamp.date;
             $scope.arr[data.thread].push(tmp);
        })
})

when I refresh page all things is okay, but when I switch to another page and come back to chat page $scope.arr not null and last data array in listener is exist.

What should I do for null array after come back to chat page (just on listener block)?

@frizar
Copy link

frizar commented Mar 16, 2017

I have problem like this ^
I have controller with chatSocket handler:
chatSocket.on('message', (data) => console.log);
First time console.log work only once.
But when i change routes, e.g. /chat -> /main -> /chat
Now my handler work TWICE.
Should i use chatSocket.removeAllListeners() before enter the /chat or ... ?

@frizar
Copy link

frizar commented Mar 16, 2017

I just install example from angular-socket-io-seed and i have same problem.

@essivision
Copy link
Author

@frizar, Do you find any solutions?

@frizar
Copy link

frizar commented Apr 13, 2017

@essivision, I just remove listeners before scope destroyed on state change.
In angular 1.5+ i use:
this.$onDestroy hook (https://docs.angularjs.org/guide/component)
In this hook i just write
chatSocket.removeListener('message');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants