I am learning AngularJS in conjunction with Firebase. I am really struggling with the on
callback of Firebase and trying to update the $scope
...
Solution 1 - Remove the $rootscope.$apply on the service and inject and apply the $timeout to the controller:
firebaseService.on('child_added',function(data){
$timeout(function(){
$scope.messages.push(data.val());
},0);
});
Solution 2 - Implement a "SafeApply" method (thanks to Alex Vanston):
$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
fn();
} else {
this.$apply(fn);
}
};