Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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 Firebase,将快照数据存储到数组变量_Javascript_Angularjs_Firebase_Firebase Realtime Database - Fatal编程技术网

Javascript Firebase,将快照数据存储到数组变量

Javascript Firebase,将快照数据存储到数组变量,javascript,angularjs,firebase,firebase-realtime-database,Javascript,Angularjs,Firebase,Firebase Realtime Database,我可以使用('child_added',function())上的ref.child('user')。将用户内部的内容存储到数组变量中 这就是我的例子 $scope.samplearray = []; ref.child('user').on("child_added", function(snapshot) { $scope.samplearray.name = snapshot.val().name; $scope.samplearray.age = snapshot.val().a

我可以使用('child_added',function())上的
ref.child('user')。将用户内部的内容存储到数组变量中

这就是我的例子

$scope.samplearray = [];
ref.child('user').on("child_added", function(snapshot) {
  $scope.samplearray.name = snapshot.val().name;
  $scope.samplearray.age = snapshot.val().age;
});

然后使用该数组在my html中显示它,Firebase数据库中的子事件是异步触发的,这在Angular 1.x中是一个开箱即用的问题。您需要在添加到数组后直接触发
$digest
循环,或者使用AngularFire

普通SDK方法

function MyController($scope) {
  var ref = firebase.database.ref('items');
  var items = [];
  ref.on('child_added', function(snap) {
    $scope.$evalAsync(function() {
      var item = snap.val();
      // use a super private property to keep around the key
      item.__key = snap.key; 
      items.push(item);
    });
  });
}
function MyController($scope, $firebaseArray) {
  var ref = firebase.database.ref('items');
  // Handles all child events: added, removed, changed, moved
  $scope.items = $firebaseArray(ref);
}
角射进近

function MyController($scope) {
  var ref = firebase.database.ref('items');
  var items = [];
  ref.on('child_added', function(snap) {
    $scope.$evalAsync(function() {
      var item = snap.val();
      // use a super private property to keep around the key
      item.__key = snap.key; 
      items.push(item);
    });
  });
}
function MyController($scope, $firebaseArray) {
  var ref = firebase.database.ref('items');
  // Handles all child events: added, removed, changed, moved
  $scope.items = $firebaseArray(ref);
}

我的回答有用吗?