9

在 angularFire 示例中,它展示了如何从 firebase 获取对象集合。

app.controller('ctrl', ['$scope', '$timeout', 'angularFireCollection',
  function($scope, $timeout, angularFireCollection) {    
    var url = 'https://ex.firebaseio.com/stuff';
    $scope.col = angularFireCollection(url);
  }
]);

只有一个对象呢?

我试过这样的事情:

  fb.child('stuff/'+id).on('value', function(snapshot) {
    $scope.obj = snapshot.val();
    console.log('hey got the value')
    console.log(snapshot.val())
  });

似乎不起作用。控制台正确输出对象值,但控制器不更新。

4

1 回答 1

9

尝试使用常规 angularFire 服务,并指定单个对象的类型:

app.controller('ctrl', ['$scope', '$timeout', 'angularFire',
  function($scope, $timeout, angularFire) {    
    var url = 'https://ex.firebaseio.com/stuff';
    angularFire(url, $scope, "obj", "");
  }
]);

注意第 4 个参数(“”表示字符串,也可以使用布尔值、数字、对象和数组)。

于 2013-04-29T05:56:05.523 回答