0

I am using the below code, I only get Question Ids, but what I need is when I click Save button I want to display questions.id, option.id and selected : true or false below the textbox. Below code is correct but want to make it as like array.

controllers.js

 $scope.save = function() {
        $scope.msg = 'Question IDs:';
        $scope.questions.forEach(function(el){
            console.log(el)
            $scope.msg += el.Id + ',';
        })
 }

in $scope.questions all the json data is stored.

home.html

<div ng-repeat="question in filteredQuestions">
            <div class="label label-warning">Question {{currentPage}} of {{totalItems}}.</div>
            <div class="row">
                <div class="row">
                    <h3>{{currentPage}}. <span ng-bind-html="question.Name"></span></h3>
                </div>
            </div>
            <div class="row text-left options">
                <div class="col-md-6" ng-repeat="option in question.Options" style="float:right;">
                    <div class="option">
                        <label class="" for="{{option.Id}}">
                           <h4> <input id="{{option.Id}}" type="checkbox" ng-model="option.Selected" ng-change="onSelect(question, option);" />
                            {{option.Name}}</h4>
                        </label>
                    </div>
                </div>
                </div>

            <div class="center"><button ng-click="save()">Save</button></div>
            <div class="center"><textarea  rows="5" cols="50">{{msg}}</textarea></div>
        </div>

csharp.js

{
    "quiz": {
        "Id": 2,
        "name": "C# and .Net Framework",
        "description": "C# and .Net Quiz (contains C#, .Net Framework, Linq, etc.)",
        "paragraph": "In 2015 Microsoft released ASP.NET 5.ASP.NET 5 is a significant redesign of ASP.NET.ASP.NET, MVC, and Web Pages are now merged into a single framework named MVC 6.It includes the following features:Linux support OSX support Node.js supportA ngularJS supportTag ,HelpersView, ComponentsWeb ,APIGruntJS ,supportBower, supportNo ,Visual BasicNo Web Forms"
    },
    "config": {
        "shuffleQuestions": true,
        "showPager": false,
        "allowBack": true,
        "autoMove": false
    },
    "questions": [{
        "Id": 1010,
        "Name": "Which of the following assemblies can be stored in Global Assembly Cache?",
        "QuestionTypeId": 1,
        "Options": [{
            "Id": 1055,
            "QuestionId": 1010,
            "Name": "Private Assemblies"
        }, {
            "Id": 1056,
            "QuestionId": 1010,
            "Name": "Friend Assemblies"
        }, {
            "Id": 1057,
            "QuestionId": 1010,
            "Name": "Public Assemblies"
        }, {
            "Id": 1058,
            "QuestionId": 1010,
            "Name": "Shared Assemblies"
        }]
    }, {
        "Id": 1019,
        "Name": "Which of the following does NOT represent Integer?",
        "QuestionTypeId": 1,
        "Options": [{
            "Id": 1055,
            "QuestionId": 1010,
            "Name": "Char"
        }, {
            "Id": 1056,
            "QuestionId": 1010,
            "Name": "Byte"
        }, {
            "Id": 1057,
            "QuestionId": 1010,
            "Name": "Short"
        }, {
            "Id": 1058,
            "QuestionId": 1010,
            "Name": "Long"
        }]

    }]
}

2 Answers 2

0

Use this code in your controller to have the question id the selected option id and non selected options id.

$scope.save = function() {
  console.log('Question IDs:' + question.id);
  $scope.questions.Options.forEach(function(option){
        if(option.selected){
           console.log('Selected Options:' +option.id)
           $scope.msg = 'Question IDs: ' + question.id + ', Selected option id: ' + option.id;
        }
        else{
           console.log('Not selected Options:' +option.id)
        }
  })
}

Hope it helps

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

3 Comments

get an error like Uncaught SyntaxError: missing ) after argument list
@Sherin I'm Sorry. Forgot to add the operator to concat + in consoles. Try again please.
yes I find that and we need to call question before using console.log(question.id). But the question Id is already in option.So I used to call option.QuestionId but it give me error like "cannot read property foreach".whats the error here..?? @mJunaidSalaat
0

You can pass the entire object to your function like this:

<div ng-repeat="question in filteredQuestions">
        <div class="label label-warning">Question {{currentPage}} of {{totalItems}}.</div>
        <div class="row">
            <div class="row">
                <h3>{{currentPage}}. <span ng-bind-html="question.Name"></span></h3>
            </div>
        </div>
        <div class="row text-left options">
            <div class="col-md-6" ng-repeat="option in question.Options" style="float:right;">
                <div class="option">
                    <label class="" for="{{option.Id}}">
                       <h4> <input id="{{option.Id}}" type="checkbox" ng-model="option.Selected" ng-change="onSelect(question, option);" />
                        {{option.Name}}</h4>
                    </label>
                </div>
            </div>
            </div>

        <div class="center"><button ng-click="save(question, option)">Save</button></div>
        <div class="center"><textarea  rows="5" cols="50">{{msg}}</textarea></div>
    </div>

And log them in javascript with this:

 $scope.save = function(question, option) {
    $scope.msg = 'Question IDs:';
    console.log(question + ':' + option) 
 }

1 Comment

But here i need to display Question Id And selected options id.if any answer is selected include selected:true otherwise selected:false. @Sjoerddewit

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.