admin管理员组

文章数量:1387461

In the angularFire examples it shows how to get a collection of objects from firebase.

app.controller('ctrl', ['$scope', '$timeout', 'angularFireCollection',
  function($scope, $timeout, angularFireCollection) {    
    var url = '';
    $scope.col = angularFireCollection(url);
  }
]);

What about a single object only?

I tried something like this:

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

Doesn't seem to work. The console outputs the object value correctly but the controller doesn't update.

In the angularFire examples it shows how to get a collection of objects from firebase.

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

What about a single object only?

I tried something like this:

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

Doesn't seem to work. The console outputs the object value correctly but the controller doesn't update.

Share Improve this question edited Apr 29, 2013 at 5:20 Andrew Lee 10.2k3 gold badges48 silver badges40 bronze badges asked Apr 29, 2013 at 0:21 HarryHarry 55.1k76 gold badges187 silver badges270 bronze badges 1
  • 1 As for why your attempt didn't work: when modifying $scope outside of an angular function (like in that Firebase callback), you need to manually trigger a $digest with $scope.$apply(). There's a pretty good explanation here: jimhoskins./2012/12/17/angularjs-and-apply.html – bennlich Commented Oct 8, 2013 at 18:50
Add a ment  | 

1 Answer 1

Reset to default 9

Try using the regular angularFire service, and specifying the type of your single object:

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

Note the 4th argument (" " means string, you can also use booleans, numbers, objects and arrays).

本文标签: javascriptAngularFire single objectStack Overflow