0

There is an array of questions that are displayed on the form. I want to get all the answers into an array that I can push to Firebase.

This question (Angularjs equivalent to serialize) is pretty similar. However, I do not have ids per question and that answer does not provide the controller code.

Here is the HTML:

 <form ng-submit="addAnswers()">
      <dd ng-repeat="questions in itemform.questions">
        {{questions}}
        <input type="text" ng-model = "answers">
      </dd>
      <input type="submit">
    </form>

Here is the controller:

phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$firebaseObject',function($scope, $firebaseObject) {

 $scope.addAnswers = function() {     
 var ans = new Firebase("https://APP.firebaseio.com/answers");
 var x = $scope.answers;
 console.log($element.serialize()); //says "element is not defined"
 console.log(x.serializeArray()); "says x is undefined

};

}]);
Community
  • 1
  • 1

1 Answers1

1

Try doing it like this:

 <form ng-submit="addAnswers()">
  <dd ng-repeat="questions in itemform.questions">
    {{questions}}
    <input type="text" ng-model = "answers[$index]">
  </dd>
  <input type="submit">
</form>

Initialize the array in the controller.

$scope.answers = [];

Here is an example http://codepen.io/mkl/pen/pyrPXB/

Michael
  • 653
  • 6
  • 19