B
B
beduin012015-11-13 20:05:13
Angular
beduin01, 2015-11-13 20:05:13

How to change the value of multiple fields in AngualarJS?

There is a Json object:

{
  "id": 1, 
  "question": "Текст вопроса?", 
    "answers": [
          {
            "id" : 1,
            "answer":	"aaa",
            "isSelected" : 0
          }, 
          {	"id" : 2,
             "answer": "bbb",
             "isSelected" : 0
          }, 
          {	 "id" : 3,
             "answer": "ccc",
             "isSelected" : 0
          }, 
          {	 "id" : 4,
             "answer": "ddd",
            "isSelected" : 0
          }
         ],
    "allowMultipleChoice" : true
}

I want to do the following. I draw all this happiness in the form of buttons. I have already learned to do this. When I click on the button, I change "isSelected" to 1. The problem is what I need to do so that not all sections have the possibility of multiple selection. Now if I click on all the buttons, then all of my isSelected will get one. The task is to somehow handle allowMultipleChoice. As I understand it, you need to check its value and if it is false, then reset the values ​​​​of all other isSelected except for the given one.
Could you please advise how to do it correctly. I just can't figure out how to bypass all isSelected

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav Lyzlov, 2015-11-13
@dixoNich

self.answers = [{...}]
self.currentAnswer = null

self.onSelectAnswer = function (answer) {
   self.currentAnswer.isSelected = true
   answer.isSelected = true
   self.currentAnswer = answer
}

I think the idea is clear. Perhaps you can just do currentAnswer = answer and in the isSelected template look like currentAnswer === answer, because everything according to the links, sl. objects are equal.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question