angularjs - $broadcast to current scope -


to preface, have ionic app connected node server via websocket , node server connected c++ app via tcp socket. have service connects , serves socket watches nack response can issue alert notifying user of error:

(function(){   'use strict';    angular.module('tacocorp.services')     .factory('socket', socket);    socket.$inject = ['socketfactory', 'osdp', '$rootscope'];    function socket(socketfactory, osdp, $rootscope) {     var socket = io('localhost:3000');      socket = socketfactory({iosocket: socket});      socket.on('connect', function() {         console.log('connected');     });      socket.on('nack', function(data) {         console.log(data);         var error = osdp.parse(data);         $rootscope.$broadcast('nack', error);     });      return socket;   } }()); 

i have in each controller listening can issue popup:

$scope.$on('nack', function(e, err) {         console.log(err);         var alertpopup = $ionicpopup.alert({             title: 'error',             template: err.error         });     }); 

my problem is firing multiple times though there 1 controller each state/view. if $destroy isn't happening or it's not removing listener. still happens if this:

var nacklistener = $scope.$on('nack', function(e, err) {         console.log(e);         var alertpopup = $ionicpopup.alert({             title: 'error',             template: err.error         });     });      $scope.$on('$destroy', nacklistener); 

i'm not sure best way handle is.

as radim köhler pointed out $ionicconfigprovider.views.maxcache(0) fixes issue cache:false on state not. this:

$scope.$on("$ionicview.afterleave", function () {         $ionichistory.clearcache();     }); 

best way me pick 1 controller put on , done it.


Comments