$广播当前范围 [英] $broadcast to current scope
问题描述
要preface,我已经通过的WebSocket和节点服务器连接到一个节点服务器离子应用通过TCP套接字连接到一个C ++应用程序。我有这个服务,连接并提供了插座,而且手表一个 NACK
的响应,这样就可以发出警报通知用户出现错误:
To preface, I have an Ionic app connected to a Node server via a websocket and the Node server is connected to a C++ app via a TCP socket. I have this service that connects and serves up the socket but also watches for a nack
response so that it can issue an alert notifying the user of the 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 this in each controller listening so that it can issue the popup:
$scope.$on('nack', function(e, err) {
console.log(err);
var alertPopup = $ionicPopup.alert({
title: 'Error',
template: err.error
});
});
我的问题是,它是射击,即使我只有一个每个国家/视图控制器多次。仿佛 $摧毁
没有发生,或者它不删除监听器。仍然发生,如果我这样做:
My problem is that it is firing multiple times even though my there is only one controller for each state/view. Almost as if $destroy
isn't happening or it's not removing the listener. Still happens if I do 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 what the best way to handle this is.
推荐答案
拉迪姆·克勒指出 $ ionicConfigProvider.views.maxCache(0)
解决这个问题,其中缓存:假
的状态不对。你也可以这样做:
As Radim Köhler pointed out $ionicConfigProvider.views.maxCache(0)
fixes this issue where cache:false
on the state does not. You could also do this:
$scope.$on("$ionicView.afterLeave", function () {
$ionicHistory.clearCache();
});
我
最好的办法是随便挑一个控制器把它放在与它做。
Best way for me is to just pick one controller to put it on and be done with it.
这篇关于$广播当前范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!