AngularJS在连接或断开连接到firebase时更新服务值

Nor*_*ldt 10 angularjs firebase angularjs-service firebase-realtime-database

我希望在连接或断开连接到firebase服务器时更改图标颜色.我到目前为止:

HTML

<button class="button button-icon ion-cloud" ng-style="dbConnectedStyle"></button>
Run Code Online (Sandbox Code Playgroud)

调节器

firebaseRef.$loaded().then( function() {
  $scope.dbConnectedStyle = {'color': dbConnectStatus.color};
}
Run Code Online (Sandbox Code Playgroud)

服务

.service('dbConnectStatus', function(firebaseRef){
  var status = false;
  var color = 'transparent';
  var connectedRef = firebaseRef.child(".info/connected");
  connectedRef.on("value", function(snap) {
    status = snap.val();
    if (status) {
      color = 'lightgrey';
      console.log("Connected to DB (" + color + ")" );
    } else {
      color = 'transparent';
      console.log("Disonnected to DB (" + color + ")" );
    }
  });
  return {
    'boolean': status,
    'color': color
  }
})
Run Code Online (Sandbox Code Playgroud)

它第一次改变颜色.但是当它断开连接时它没有改变......似乎它不是双向绑定到服务.我该如何实现这一目标?


UPDATE

尝试将服务作为对象进行引用而不是执行基元分配,如在Angular.js中的Frankenstein和绑定到服务值的好教程中所解释的那样

我将代码更改为以下内容

HTML

<button class="button button-icon ion-cloud" 
        ng-style="dbConnectionStatus.connectionStyle">
        </button>
Run Code Online (Sandbox Code Playgroud)

服务

.service('dbConnectStatus', function(firebaseRef, $rootScope){
  this.status = false;
  var styles = {
    'offlineStyle': {'color': 'red'},
    'onlineStyle': {'color': 'lightgrey'}
  };
  this.connectionStyle = styles.offlineStyle;

  firebaseRef.child(".info/connected")
    .on("value",
      function(snap) {
        this.status = snap.val();
        if (snap.val()) {
          console.log("Connected to DB.");
          this.connectionStyle = styles.onlineStyle;
          console.log(this.connectionStyle);
        } else {
          console.log("Disconnected to DB.");
          this.connectionStyle = styles.offlineStyle;
          console.log(this.connectionStyle);
        }
        console.log(this.status);
        $rootScope.$broadcast('dbConnection:changed');            
      }
    );

})
Run Code Online (Sandbox Code Playgroud)

调节器

$scope.dbConnectionStatus = dbConnectStatus;
      $scope.$on('dbConnection:changed', function() {
        console.log("'on(...)' called. This is the $scope.dbConnectionStatus.connectionStyle:");
        $scope.dbConnectionStatus = dbConnectStatus;
        console.log($scope.dbConnectionStatus.connectionStyle);
        console.log("This is the dbConnectStatus.connectionStyle:");
        console.log(dbConnectStatus.connectionStyle);
      });
      $rootScope.$watch('dbConnectStatus', function (){
        $scope.dbConnectionStatus = dbConnectStatus;
      });
      //$rootScope.$apply();
Run Code Online (Sandbox Code Playgroud)

然后我重新加载代码并获得此控制台消息

第一次加载

然后我关闭了连接

互联网连接关闭

然后我打开连接

互联网连接

我很清楚,该服务dbConnectionStatus不会像我预期的那样更新为全局变量.我假设在应用程序加载时调用一次服务,并且将范围变量分配给服务(对象)不是调用而是引用...

我究竟做错了什么?

ado*_*srs 5

我在jsFiddle中使用$emit$on处理服务中的状态更改.主要的问题是,当上线时角度绑定不能正常工作,所以我需要强制进行角度循环$scope.$apply().

我开始研究代码的第一个版本,但进行了一些重构.您可以在jsFiddle上找到完整代码,但服务和控制器如下所示:

服务

.service('dbConnectStatus', function($rootScope){
  var status = false;
  var color = 'red';
  var self = {      
        startWatchingConnectionStatus: function(){
        var connectedRef = firebase.database().ref().child(".info/connected");
        connectedRef.on("value", function(snap) {
            console.log(snap.val());
            status = snap.val();
            if (status) {
                color = 'blue';
                console.log("Connected to DB (" + color + ")" );
            } else {
                color = 'red';
              console.log("Disonnected to DB (" + color + ")" );
              }
            $rootScope.$emit('connectionStatus:change', {style: {'color': color}, status: status}});
        });
      },
      getStatus: function(){
        return status;
      },
      getColor: function(){
        return color;
      }
  };
  return self;
})
Run Code Online (Sandbox Code Playgroud)

调节器

.controller('HomeCtrl', ['$scope', 'dbConnectStatus', '$rootScope',function($scope, dbConnectStatus, $rootScope) {

    dbConnectStatus.startWatchingConnectionStatus();

    $rootScope.$on('connectionStatus:change',  function currentCityChanged(event, value){
        $scope.color = value.style;
        //if changed to connected then force the $apply
        if(value.status === true){
            $scope.$apply();
        }
    });  
}]);
Run Code Online (Sandbox Code Playgroud)

如果有任何事情仍然不清楚,请告诉我.