Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS绑定仅在更改后更新_Javascript_Angularjs_Firebase - Fatal编程技术网

Javascript AngularJS绑定仅在更改后更新

Javascript AngularJS绑定仅在更改后更新,javascript,angularjs,firebase,Javascript,Angularjs,Firebase,您好,我已经创建了一个工厂,从我的Firebase数据库获取当前在线用户数量 当我第一次加载页面时,它工作得很好,并显示所有当前用户,但如果我转到另一个页面并返回,它将显示为0,直到新用户连接或断开连接,或者如果我刷新 我遵循了这个指南: App.js angular.module('myApp', ['ngRoute', 'firebase', 'ui.bootstrap']) .factory('PresenceService', ['$rootScope', function($ro

您好,我已经创建了一个工厂,从我的Firebase数据库获取当前在线用户数量

当我第一次加载页面时,它工作得很好,并显示所有当前用户,但如果我转到另一个页面并返回,它将显示为0,直到新用户连接或断开连接,或者如果我刷新

我遵循了这个指南:

App.js

angular.module('myApp', ['ngRoute', 'firebase', 'ui.bootstrap'])
.factory('PresenceService', ['$rootScope',
  function($rootScope) {
    var onlineUsers = 0;

    // Create our references
    var listRef = new Firebase('https://my-db.firebaseio.com/presence/');

    // This creates a unique reference for each user
    var onlineUserRef = listRef.push(); 
    var presenceRef = new Firebase('https://my-db.firebaseio.com/.info/connected');

    // Add ourselves to presence list when online.
    presenceRef.on('value', function(snap) {
      if (snap.val()) {
        onlineUserRef.set(true);
        // Remove ourselves when we disconnect.
        onlineUserRef.onDisconnect().remove();
      }
    });

    // Get the user count and notify the application
    listRef.on('value', function(snap) {
      onlineUsers = snap.numChildren();
      $rootScope.$broadcast('onOnlineUser');
    });

    var getOnlineUserCount = function() {
      return onlineUsers;
    }

    return {
      getOnlineUserCount: getOnlineUserCount
    }
  }
]);
angular.module('myApp')
.controller('mainController', function($scope, authService, PresenceService, $http, $routeParams, $firebaseObject, $firebaseAuth, $location) {

  $scope.totalViewers = 0;

  $scope.$on('onOnlineUser', function() {
    $scope.$apply(function() {
      $scope.totalViewers = PresenceService.getOnlineUserCount();
    });
  });

  // login section and auth
  var ref = new Firebase("https://my-db.firebaseio.com");
  $scope.authObj = $firebaseAuth(ref);
  var authData = $scope.authObj.$getAuth();
  if (authData) {
    console.log("Logged in as:", authData.uid);
    $location.path( "/user/"+authData.uid );
  } else {
    console.log("Logged out");
    $location.path( "/" );
  }

  // user ref
  var userRef = new Firebase("https://my-db.firebaseio.com/users/"+ authData.uid);
  var syncObject = $firebaseObject(userRef);
  syncObject.$bindTo($scope, "data");

});
mainController.js

angular.module('myApp', ['ngRoute', 'firebase', 'ui.bootstrap'])
.factory('PresenceService', ['$rootScope',
  function($rootScope) {
    var onlineUsers = 0;

    // Create our references
    var listRef = new Firebase('https://my-db.firebaseio.com/presence/');

    // This creates a unique reference for each user
    var onlineUserRef = listRef.push(); 
    var presenceRef = new Firebase('https://my-db.firebaseio.com/.info/connected');

    // Add ourselves to presence list when online.
    presenceRef.on('value', function(snap) {
      if (snap.val()) {
        onlineUserRef.set(true);
        // Remove ourselves when we disconnect.
        onlineUserRef.onDisconnect().remove();
      }
    });

    // Get the user count and notify the application
    listRef.on('value', function(snap) {
      onlineUsers = snap.numChildren();
      $rootScope.$broadcast('onOnlineUser');
    });

    var getOnlineUserCount = function() {
      return onlineUsers;
    }

    return {
      getOnlineUserCount: getOnlineUserCount
    }
  }
]);
angular.module('myApp')
.controller('mainController', function($scope, authService, PresenceService, $http, $routeParams, $firebaseObject, $firebaseAuth, $location) {

  $scope.totalViewers = 0;

  $scope.$on('onOnlineUser', function() {
    $scope.$apply(function() {
      $scope.totalViewers = PresenceService.getOnlineUserCount();
    });
  });

  // login section and auth
  var ref = new Firebase("https://my-db.firebaseio.com");
  $scope.authObj = $firebaseAuth(ref);
  var authData = $scope.authObj.$getAuth();
  if (authData) {
    console.log("Logged in as:", authData.uid);
    $location.path( "/user/"+authData.uid );
  } else {
    console.log("Logged out");
    $location.path( "/" );
  }

  // user ref
  var userRef = new Firebase("https://my-db.firebaseio.com/users/"+ authData.uid);
  var syncObject = $firebaseObject(userRef);
  syncObject.$bindTo($scope, "data");

});
main.html

{{totalViewers}}

在您的控制器内,更改您的第一行,如下所示

 //$scope.totalViewers = 0;
$scope.totalViewers = PresenceService.getOnlineUserCount();
因为每次离开页面时,它的控制器都会刷新,下次它的值为“零”。因此,正确地说,您应该从您的服务中读取$scope.totalViewers