0
$scope.questions =
[
        {
            "id": 1, 
            "question": "Who are you?", 
                "answers": [
                            {
                                "id" : 1,
                                "answer":   "asd",
                                "isSelected" : 1
                            }, 
                            {   "id" : 2,
                                 "answer": "zxc",
                                 "isSelected" : 1
                            }, 
                            {    "id" : 3,
                                 "answer": "Мrty",
                                 "isSelected" : 1
                            }

                         ],
                "allowMultipleChoice" : true,
                "isEnabled" : true,
                "NowSelected" : 0
        },

        {
            "id": 2, 
            "question": "New question?", "answers": [
                                                                                                {
                                                                                                    "id" : 1,
                                                                                                    "answer": "Personal",
                                                                                                    "isSelected" : 0
                                                                                                }, 
                                                                                                {
                                                                                                    "id" : 2,
                                                                                                    "answer": "Goverment",
                                                                                                    "isSelected" : 0
                                                                                                }
                                                                                              ],
            "allowMultipleChoice" : true,
            "isEnabled" : true,
            "NowSelected" : 0
        },

        {
            "id": 3, "question": "What is your name?", "answers": [
                                                                    {"id": 1, "answer" : "Dima", "isSelected" : 0}, 
                                                                    {"id": 2, "answer": "Roma", "isSelected" : 0}, 
                                                                    {"id": 3, "answer": "Masha", "isSelected" : 0}],
            "allowMultipleChoice" : false,
            "isEnabled" : true,
            "NowSelected" : 0
        },
        {
            "id": 4, "question": "Where do you live?", "answers": ["Moscow", "London", "Minsk"],
            "isEnabled" : true,
            "NowSelected" : 0
        }
]

Мне нужно добраться до вложенного элемента isSelected и поменять его значение. Что-то делаю не так во вложенном цикле. Подскажите в чем ошибка

        angular.forEach($scope.questions, function(question)
        {
            console.log(question);
            angular.forEach(question.answers, function(val))
            {
                //question.answers или что должно быть?
            }
        }

            )

1 ответ 1

0

Во второй forEach неправильно закрыли скобки. Так лучше:

angular.forEach($scope.questions, function (question) {
    angular.forEach(question.answers, function (answer) {
        console.log(answer.isSelected);
    });
});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.