Firebase callbacks and AngularJS

前端 未结 1 889
野性不改
野性不改 2020-12-06 03:46

I am learning AngularJS in conjunction with Firebase. I am really struggling with the on callback of Firebase and trying to update the $scope...

相关标签:
1条回答
  • 2020-12-06 04:13

    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);
            }
        };
    
    0 讨论(0)
提交回复
热议问题