angularjs - angular controller multiple times -
i have web page angular in using ngroute redirect 3 pages.
modulo_angular.config(function($routeprovider){ //config y defi de las rutas $routeprovider.when("/", { controller: "appuom", controlleras: "apuom", templateurl: "home.html" }) .when("/descargas", { controller: "appsocket", controlleras: "apsocket", templateurl: "descargas.html" }) .when("/opciones", { controller: "appphenomena", controlleras: "apphen", templateurl: "opciones.html" }); });
i have 3 factories , 3 different controllers. each factory related 1 controller can keep data when page changed.
in 1 page, "descargas", have sockets receive data ym node.js server. controller:
function controladorsocket($http, factorycontroller, socket){ var vm = this; vm.mensajes = factorycontroller.getmensajes(); socket.on('mensaje', function(data){ console.log(data.message); factorycontroller.addmensaje(data.message); }); }
and this, socket factory:
modulo_angular.factory('socket', function ($rootscope) { var socket = io.connect(); return { on: function (eventname, callback) { socket.on(eventname, function () { var args = arguments; $rootscope.$apply(function () { callback.apply(socket, args); }); }); } }; });
each time switch descargas.html page , come back, receive socket messages as changed have done until moment.
so think problem comes because socket.on('mensaje'... function should go in place avoit this. don't don't know how it.
you'll have destroy socket manually on page change:
function controladorsocket($http, factorycontroller, socket){ var vm = this; vm.mensajes = factorycontroller.getmensajes(); socket.on('mensaje', function(data){ console.log(data.message); factorycontroller.addmensaje(data.message); }); this.$on('$destroy', function(){ //destroy socket }); }
Comments
Post a Comment