AngularFire从Firebase获得最新评估的$ value

时间:2016-10-21 05:52:05

标签: angularjs firebase firebase-realtime-database angularfire

我需要一些帮助才能从angularjs中获取Firebase的最新评估$值。我只能获得返回值' 1'这与评估后的预期$值不同。不知何故,html中的$值反映了预期值。

Firebase DB

-- TraineeCounter: 28

这是我的firebase工厂:

    /// * found that this factory caused the issue of value returns 1
function TraineesCounter($firebaseObject) {
    var ref = firebase.database().ref().child("TraineeCounter");
    ref.transaction(function(currentValue) {
        return (currentValue || 0) + 1;
    });

    return $firebaseObject(ref);
}

// defined wif pad trainees counts factory
function TraineesCount($firebaseObject){
    var ref = firebase.database().ref().child("TraineeCounter");
        // return it as a synchronized object
    return $firebaseObject(ref);
}

然后,我将它注入控制器,如下所示:

$scope.TraineesCounter = TraineesCounter;
$scope.traineeCount = TraineesCount;

$scope.traineeCount.$loaded().then(function(data) {
    console.log("$loaded data", data);
    console.log("$loaded value", data.$value);
});

控制台显示:

$loaded data 
  e {$$conf: Object, $id: "wifPadTraineeCounter", $priority: null, $value: 1}
  $$conf: Object
  $id: "wifPadTraineeCounter"
  $priority: null
  $value: 28
  __proto__: Object

$loaded value 1

的index.html:

    <pre>{{traineeCount | json}}</pre>
    <span>{{traineeCount.$value}}</span>

HTML输出:

{
  "$id": "wifPadTraineeCounter",
  "$priority": null,
  "$value": 27
}
27

0 个答案:

没有答案