AngularJS回响非常快速的变化

时间:2016-08-04 06:39:05

标签: angularjs websocket angularjs-scope

您好我正在使用有角度的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}}无法正常工作,这意味着,我们有两种可能的情况

  1. {{connection_status}}在线(正确)且$ rootScope.connection_status在线
  2. {{connection_status}}正在连接(不正确),但$ rootScope.connection_status在线为什么?

0 个答案:

没有答案