I'm new to AngularJS so please be gentle.
I'm trying to fetch data from firebase using a factory .on('value',..) and pass it through to the controller. It seems to be a problem with passing the object itemObj through to the controller, because line 25 console.log(itemObj.name)
shows us what we're looking for.
the factory:
'use strict';
angular.module('MyApp')
.factory('MenuItems', function(FIREBASE_ROOT) {
var ref = new Firebase(FIREBASE_ROOT);
var refMenu = ref.child('/menu/');
// get today's date in dd-mm-yyyy
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) { dd='0'+dd; }
if(mm<10) { mm='0'+mm; }
today = dd+'-'+mm+'-'+yyyy;
var itemObj = null;
return {
items: function() {
refMenu.child(today).on('value', function(snapshot) {
itemObj = snapshot.val();
console.log(itemObj.name);
return itemObj;
});
}
};
});
the controller:
'use strict';
angular.module('MyApp.controllers').controller('DashboardCtrl', ['$scope', '$firebase', 'FIREBASE_ROOT', 'Auth', 'User', 'MenuItems',
function($scope, $firebase, FIREBASE_ROOT, Auth, User, MenuItems) {
var ref = new Firebase(FIREBASE_ROOT);
var refCart = ref.child('/cart/');
$scope.numberItems = 0;
var user = Auth.currentUser;
$scope.item = MenuItems.items();
console.log(MenuItems.items());
}]);