$ scope.watch在两个不同的视图中

时间:2017-11-10 13:42:15

标签: angularjs firebase web-applications firebase-realtime-database

我正在开发一个使用Firebase作为后端的Footballing angular JS webb应用。下面是一个视图的JS:

 databaseService.users.child(user.uid).once('value', function(usersSnapshot){
    var users = usersSnapshot.val();
    var selection = users.selection;
    var team = users.teamname;
    var week1 = users.week1;
    var week2 = users.week2;
    var week3 = users.week3;
    var week4 = users.week4;
    var week5 = users.week5;
    var week6 = users.week6;
    var week7 = users.week7;
    var week8 = users.week8;
    var week9 = users.week9;
    var week10 = users.week10;
    var week11 = users.week11;
    var week12 = users.week12;

 $scope.$watch('players', function (newValue, oldValue) {
    $scope.totalPoints =(newValue, 'goals');
    firebase.database().ref("users").child(user.uid).child("week12");.set($scope.totalPoints);

},true);

所以基本上玩家每周目标都会被观看并反映在视图中,之后玩家的更新目标总数为$set到火力点节点。

下面是用于更新赛季目标总数的代码行:

 var addition = week1 + week2 + week3 + week4 + week5 + week6 + week7 + week8 + week9 + week10 + week11 + week12;
      var seasonTotal = firebase.database().ref("users").child(user.uid).child("total");


    seasonTotal.set(addition);

在第二个视图中,我有以下代码来显示firebase.database()的值.ref(" users")。child(user.uid).child(" total& #34); :

  databaseService.users.child(user.uid).on('value', function(usersSnapshot){
    var users = usersSnapshot.val();
    var total = users.total;
$scope.total = total;

我的问题

我的问题是,当我直接更改我的firebase数据库每周节点中的玩家的目标时(如每周所做的那样),目标更新会实时反映在第一个视图中。但是,当我在第二个视图中,并且我在我的数据库中进行更改时,更改更新并未实时反映(本周的总目标计数);页面需要刷新,或者我需要返回第一个视图然后返回第二个视图。

我理解这是因为将每周目标加起来的代码在第一个JS而不是第二个视图的JS中。但是有没有办法让季节总数更新,而不管用户在哪个页面?

0 个答案:

没有答案