AddItemPageController.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. app.controller('AddItemPageController',['$scope','$timeout',function($scope,$timeout){
  2. $scope.resObj = {
  3. mealName: '',
  4. type: '',
  5. weight: '',
  6. ingredients: [,,,],
  7. imageUrl: '',
  8. price: '',
  9. isSelected: false
  10. };
  11. $scope.resObj2 = {};
  12. $scope.isFirstTab = true;
  13. var databaseMeals = firebase.database().ref().child('meals');
  14. var databaseDiets = firebase.database().ref().child('diets');
  15. //functions
  16. $scope.deleteIngredient = function(i){
  17. $scope.resObj.ingredients.splice(i,1);
  18. }
  19. $scope.addIngredient = function(){
  20. $scope.resObj.ingredients.push('');
  21. }
  22. var firebaseUsersTest = firebase.database().ref().child('users');
  23. firebaseUsersTest.on('value', function(snap){
  24. $scope.usersTest = snap.val();
  25. })
  26. $scope.sendMeal = function(){
  27. var userKeys = Object.keys($scope.usersTest);
  28. console.log(userKeys)
  29. for (let i = 0; i < userKeys.length; i++) {
  30. firebaseUsersTest.child(userKeys[i]).child('meals').push().set(Object.assign($scope.resObj));
  31. }
  32. var databaseMeals2 = firebase.database().ref().child('meals');
  33. databaseMeals2.push().set($scope.resObj);
  34. }
  35. $scope.sendDiet = function(){
  36. databaseDiets.push().set($scope.resObj2);
  37. }
  38. }])