AngularFire单个对象

时间:2013-04-29 00:21:34

标签: javascript angularjs firebase angularfire

在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())
  });

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

1 个答案:

答案 0 :(得分:9)

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

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

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