i'm new both angular & firebase. i've been trying create factory update list of stations list won't seem update. when run factory code firebase.database...
in controller 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 doing wrong? isn't ng-repeat="(key,station) in stations"
list supposed change after factory returns new data ?
also i've been noticing in few tutorials. difference between 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(); }])
since didn't wrap firebase result angular promise, angular environment cannot notice there new results arrived, have 2 solutions:
using angular fire provide angular bindings firebase
wrap return result
$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; } } })
Comments
Post a Comment