0

When creating a new poll, the question and all the potential answers need to be send to the controller once submitted. I know how to work with ng-model, but I have no clue how I can bind it to an array in that array.

For example, my array looks like this:

 var question = {
    "question":"",
    "choices": [
      {
        "id":1,
        "choice": "Yes"
      },
      {
        "id"=2,
        "choice": "No"
      }
    ]
  }

Form

      <form novalidate name="questionForm" ng-submit="addVote(vote)">
        <input placeholder="Type your question..." name="question" ng-model="vote.question" />
        <input ng-model="" value="Choice 1" />
        <input ng-model="" value="Choice 2" />
<button type='submit' class="circle-btn btn-send ion-ios-paperplane"></button>
      </form>

The input[name=question] will obviously send whatever is in that input box to vote.question. But how do I add those potential answers to the choices array?

Note: it's possible to add more input fields

EDIT

So in short: I need to be able to bind {id:1, choice: value of input}

2 Answers 2

2

You can make your vote variable an object literal that contains the question and the array of answer choices (could be empty array):

In the controller:

$scope.vote = {
  "question": 'question stem title here',
  "answerChoices": [
    {"id": 1, "choice": 'Yes'},
    {"id": 2, "choice": 'No'},
  ]
};

In the HTML:

<form novalidate name="questionForm" ng-submit="addVote(vote)">
        <input placeholder="Type your question..." name="question" ng-model="vote.question" />
        <span ng-repeat="choice in vote.answerChoices track by $index">
          <input class="form-control" ng-model="vote.answerChoices[$index]" />
        </span>
<button type='submit' class="circle-btn btn-send ion-ios-paperplane"></button>
      </form>

You can push the new answer choices to the vote.answerChoices array programmatically

Sign up to request clarification or add additional context in comments.

6 Comments

Why use vote.answerChoices[$index]to refer to the current choice? That's what the choice variable is for.
Because OP said Note: it's possible to add more input fields. The vote.answerChoices[$index] is used to track the changes of the answer choices, but not for referring to the current choice.
If you add more fields, the ng-repeat will have one more element displayed, and the current element in the loop is still choice.
But I don't need to display anything, I have to send my new vote to the database. So if I use an ng-repeat then it won't show anything because the answerChoices array is empty
I don't see the OP has ever said current element or choice. I assume from his code that he also needs ng-model for each answer choice, and that is another reason I use vote.answerChoices[$index]
|
1

Just use ng-repeat to iterate on the choices, and display one input per element of the array, bound to the choice field of this element:

<div ng-repeat="element in vote.choices">
  <input ng-model="element.choice" />
</div>

3 Comments

The ng-repeat won't display anything because the array in the array is empty at first.
Then make sure it's not. Prepopulate the array with two choices, or add a button that adds an element to the choice array
Oh, this is actually a better solution! Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.