AngularFire & Ionic - No data form is transmitted

2019-09-02 08:15发布

问题:

I use Ionic and AngularFire for make my app, but I have a (new) problem: My data form are empty ! We can see just the fiels in my DataBase Firebase. How do I do to retrieve the form data entered by the user in the Firebase database?

The issue in image :

http://img15.hostingpics.net/pics/238594event.png

This is my HTML :

<form ng-submit="submitEvent(event)">
  <label class="item item-input item-stacked-label">
    <span class="input-label"><i class="fa fa-pencil"></i> Nom de l'événement</span>
    <input type="text" ng-model="event.nameid">
  </label>
  <label class="item item-input item-stacked-label">
    <span class="input-label"><i class="icon ion-ios-information"></i> Description</span>
    <br><textarea ng-model="event.descriptionid"></textarea>
  </label>
  <label class="item item-input item-stacked-label">
    <span class="input-label"><i class="fa fa-location-arrow"></i> Adresse</span>
    <input type="text" ng-model="event.addressid">
  </label>

  <button class="button" type="submit" value="Add Event" id="success-btn-create"><i class="fa fa-arrow-right"></i></button></form> 

This is my Controller JS :

myApp.controller('Step1Ctrl', ['$scope', 'Event', 'Auth', '$rootScope', '$state', '$firebaseArray', function($scope, Event, Auth, $rootScope, $state, $firebaseArray) {

  $scope.submitEvent = function(event) {

    var nameid = $scope.nameid;
    var eventRef = new Firebase("https://myApp.firebaseio.com/Events");
    eventRef.push($scope.event);
    $scope.event = {nameid: '', descriptionid: '', adressid: ''};

  }

  $scope.deleteEvent = function(index) {
    $scope.events.splice(index , 1);
  }


}])

This is my Service JS :

myApp.factory("Event", ["$firebaseArray", function($firebaseArray) {
var eventRef = new Firebase('https://myApp.firebaseio.com/Events/');
  return $firebaseArray(eventRef);
}]);

An optional question : How to recover a name of the organizer ? He is logged with Facebook ...

Thanks all folks !

回答1:

add this to your controller:

$scope.submitEvent = function(event) {

   Event.add(event).then(function (data){ 
      $scope.event = {nameid: null, descriptionid: null, adressid: null};
     });
  }

you can also reorganize your service as follows:

myApp.factory("Event", ["$firebaseArray", function($firebaseArray) {
  var eventRef = new Firebase('https://myApp.firebaseio.com/Events/');
  return {
         get: function (){
                return $firebaseArray(eventRef);
              }
            ,
        add: function (event){
          var eventInfo = $firebaseArray(eventRef);
          return eventInfo.$add(event);
              }
       }
}]);

hope this helps!