您好我正在使用有角度的websocke 当我打开websocket并打开连接或关闭连接时,$ rootScope模型没有改变。 服务代码:
.factory("MyData", function($websocket, $rootScope) {
return {
open: function () {
$rootScope.connection_status = 'connecting';
window.dataStream = $websocket(Config.socketUrl);
dataStream.onError(function () {
console.log('error')
})
dataStream.onOpen(function () {
$rootScope.connection_status = 'online';
console.log('now online',$rootScope.connection_status)
})
dataStream.onClose(function () {
$rootScope.connection_status = 'offline';
console.log('now offline',$rootScope.connection_status)
})
},
close: function () {
dataStream.close()
}
};
})
控制器:
.controller('SocketController', function ($scope, MyData) {
$scope.MyData = MyData
$scope.MyData.open()
})
观点:
{{ connection_status }}
<div ng-switch="connection_status">
<a ng-switch-when="offline" ng-click="MyData.open()">Connect</a>
<a ng-switch-when="online" ng-click="MyData.close()">Disconnect</a>
<a ng-switch-when="connecting" ng-click="MyData.close()">cancel</a>
</div>
在此代码中{{connection_status}}无法正常工作,这意味着,我们有两种可能的情况