0

i'm new with both Angular & Firebase. I've been trying to create a factory to update my list of stations but the list just won't seem to update. When i run the factory code firebase.database... in my controller it works fine though.

controller

.controller('DashCtrl', function ($scope,Stations) {

        $scope.stations = [];
        $scope.stations = Stations.getStations();
    })

services

.factory('Stations', function() {
    return {
        getStations : function(){
            firebase.database().ref('stations').once('value',function(snapshot){
                console.log(snapshot.val());
                return snapshot.val();
            })
        }
    }
})

What am i doing wrong? Isn't the ng-repeat="(key,station) in stations" list supposed to change after the factory returns the new data ?

Also I've been noticing something in a few tutorials. What is the difference between the below 2 inits.

.controller('DashCtrl', function ($scope,Stations) {

        $scope.stations = [];
        $scope.stations = Stations.getStations();
    })

.controller('DashCtrl', [$scope,Stations,function ($scope,Stations) {

        $scope.stations = [];
        $scope.stations = Stations.getStations();
    }])
1
  • Just to make it clear for the edit, the problem isn't with the databaes not returning anything rather than updating the ng-repeat list on return Commented Aug 8, 2016 at 15:05

2 Answers 2

1

since you didn't wrap the firebase result with an Angular promise, the angular environment cannot notice there are new results arrived, you have two solutions:

  1. using Angular Fire which provide angular bindings for firebase

  2. wrap the return result with an $q promise:

    //controller 
    .controller('DashCtrl', function ($scope,Stations) {
        $scope.stations = [];
        Stations.getStations().then(function(results){
            $scope.stations = results;
        });
     })
    
    //service
    .factory('Stations', function($q) {
        return {
            getStations : function(){
                var defer = $q.defer();
                firebase.database().ref('stations').once('value').then(function(snapshot){
                    defer.resolve(snapshot.val());
                }).catch(function(error){
                    defer.reject(error);
                })
                return defer.promise;
            }
        }
    })
    
Sign up to request clarification or add additional context in comments.

1 Comment

ok I got it, but i'll be using this one if i want to use once. What about on ? how could i let know the controller to update the list from the Stations factory ?
1

controller #1

.controller('DashCtrl', function ($scope,Stations) {

    $scope.stations = [];
    $scope.stations = Stations.getStations();
})

controller #2

.controller('DashCtrl', [$scope,Stations,function ($scope,Stations) {

    $scope.stations = [];
    $scope.stations = Stations.getStations();
}])

controller #2 is better for minification done by browsers which will result something like:

controller2.min.js

.controller('DashCtrl', [a,b, function(a,b) {

a.stations = [];
a.stations = b.getStations();
}])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.