Generating forms dynamically using JSON data

Currently, I'm using three nested ng-repeat directives to display multiple questions and their corresponding answers. Everything is displaying correctly so far, but now I need to create a form to save the answers. What would be the best approach to achieve this? I've been experimenting with the following code:

<form>
    <div class="panel panel-default" ng-repeat="section in questionsTest">
        <div class="panel-heading">
            <h1>{{ section.name }}</h1>
            <h3 class="panel-title">{{ section.name }}. {{
                section.description }}</h3>
        </div>
        <div class="panel-body" ng-repeat="question in section.questions">
            {{ question.statement }}
            <div ng-repeat="answer in question.answers">
                <label class="radio-inline"> <input type="{{ answer.type }}" ng-model="test.idRadioButton"
                    name="{{ question.id }}" id="{{ answer.id }}" value="{{ answer.id }}">
                    {{ answer.valor }}
                </label>
            </div>
        </div>
    </div>
</form>

Here's the JSON data structure for reference:

{
    "section": [{
        "name": "Section 1",
        "questions": [{
            "statement": "First question",
            "answers": [{
                "valor": "1",
                "id": 3,
                "type": "radio"
            }, {
                "valor": "2",
                "id": 4,
                "type": "radio"
            }, {
                "valor": "3",
                "id": 7,
                "type": "radio"
            }, {
                "valor": "4",
                "id": 8,
                "type": "radio"
            }, {
                "valor": "5",
                "id": 9,
                "type": "radio"
            }, {
                "valor": "6",
                "id": 10,
                "type": "radio"
            }, {
                "valor": "7",
                "id": 11,
                "type": "radio"
            }],
            "id": 1
        }, {
            "statement": "Question 3",
            "answers": [{
                "valor": "Yes",
                "id": 1,
                "type": "radio"
            }, {
                "valor": "No",
                "id": 2,
                "type": "radio"
            }],
            "id": 3
        }, {
            "statement": "Question 4",
            "answers": [{
                "valor": "Yes",
                "id": 1,
                "type": "radio"
            }, {
                "valor": "No",
                "id": 2,
                "type": "radio"
            }],
            "id": 4
        }],
        "description": "Description",
        "id": 1
    }

However, I'm encountering an issue where clicking on one question selects all the others as well. As a newcomer to Angular, I'm unsure how to resolve this. Any guidance would be greatly appreciated. Thanks!

I have managed to make it 'work', but I am struggling to store all the clicked radio buttons. Here's what my controller looks like:

$scope.question = {};
    $scope.testing = function(){
        console.log($scope.question);
    };

The testing function is simply used to display the values stored in $scope.question on the console.

Answer №1

It is important to utilize a unique model for each individual question.

For example, implement:

ng-model="question.value"

An interactive illustration can be found here: https://plnkr.co/edit/SMYqvUNqXNJkW9x9ZmsB?p=preview

Furthermore, if you find yourself perplexed by the functionality of the radio buttons, remember that they are designed to only allow one selection at a time. If multiple selections are necessary, consider changing the type to checkbox.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering the maximum value in AngularJS using an expression (complete with a bug demonstration)

How can I set a maximum value for an input that can be either a numeric amount or a percentage? Is it possible to use ng-max to set the max value to 100 if the input is a percentage, and leave it blank if it's a numeric amount? I attempted the follo ...

Don't display the title if there is no query made in Angular

At the moment, my current setup looks like this: <title ng-bind-template="FuturePhones: {{query}}">FuturePhones</title> However, this results in the title displaying as follows when the page loads: FuturePhones: When I query my controlle ...

Modifying certain elements in a text to emphasize (PHP)

Running JavaScript code smoothly is not a problem for me, but unfortunately, I am facing difficulties when it comes to PHP. I wish to change certain words in the text for emphasis. How can I achieve this? var terms = 'term1#term2#term3'; ...

Utilize Google Sheets to extract information from a web address containing quotation marks

I am currently utilizing a script called "ImportJSON" developed by paulgambill https://gist.github.com/paulgambill/cacd19da95a1421d3164 The URL I am working with contains quotes characters For instance: http://SomeAPIULR?{"Type": "SomeType"}&APIKE ...

What is the best method for incorporating meta data, titles, and other information at dynamic pages using Node.js and Express?

I am currently exploring methods to efficiently pass data to the html head of my project. I require a custom title, meta description, and other elements for each page within my website. Upon initial thought, one method that comes to mind is passing the da ...

The error message "TypeError: e.preventDefault is not a function" indicates that

I'm fairly new to JavaScript and jQuery, and I've incorporated two jQuery plugins into my code - jQuery form validator and jQuery form ajaxSubmit. Initially, everything was working fine with normal AJAX submission until I needed to post a file, w ...

What is the best way to incorporate a search bar into a dropdown menu?

Would someone be able to assist me in adding a search bar as the primary value of the dropdown menu? I am using ASP.NET MVC and this is my current code snippet. <div class="col-md-8"> <div class="dropdown"> <div class="chzn-d ...

The mystery behind the enigmatic combination of ajax, JQuery,

Seeking Assistance! All fields are displaying undefined values function UpdateData(){ var id = $('#id').attr('value'); var name = $('#name').attr('value'); var department = $('#departament'). ...

Tapping on the invisible picture

I currently have a square image of a car with a transparent background. My goal is to make the car clickable so that when I click on it, it triggers an action. However, I also want the transparency around the car to allow clicks to go through and affect th ...

The data in the loaded angularjs 2 select box is not showing up in the browser

I am encountering an issue with a select box that is being populated from a database. Although the select box template is loading in the browser with all the data from the database, it is not visible in the UI within a table. Controller.js: $scope.GetCvS ...

Unable to transfer information from the Parent component to the Child component

Can you help me solve this strange issue? I am experiencing a problem where I am passing data from a parent component to a child component using a service method that returns data as Observable<DemoModel>. The issue is that when the child component ...

Is it possible to refresh resources in Node.js without the need to restart the server?

Is there a middleware or library that allows access to files added after the server starts without requiring a restart? I currently use koa-static-folder, but it seems unable to handle this functionality. ...

Switch between Fixed and Relative positions as you scroll

Just a small adjustment is needed to get it working... I need to toggle between fixed and relative positioning. JSFiddle Link $(window).on('scroll', function() { ($(window).scrollTop() > 50) ? ($('.me').addClass('fix ...

Tips for updating checkbox values in the database to 1 when selected and 0 when deselected

Managing Database Information <?php if(isset($_POST["insert"])) { $conn = mysqli_connect("localhost", "root", "", "databaseappfeature"); if(isset($_POST["insert"]) == "1"){ $query = "UPDATE appfeature SET feature_switch = ('".$_POST["ins ...

Adjust the vertical alignment of an HTML element

I've been trying to make an image move using the variable counter, but for some reason, it's not working. If I set document.getElementById("movee").style.top directly to a number, it works fine. Combining it with the counter variable should theor ...

Managing extensive amounts of data with server-side scripting in a Datatable

I am exploring the use of the datatable plugin to effectively manage a large amount of data. Currently, I am interested in implementing "server side processing in datatables" with the help of server-side scripting. Since I have limited experience with AJA ...

Utilize the WebGLRenderTarget again

In my project, I am working with two scenes: the main scene which displays a textured plane, and a secondary scene that needs to be rendered to a texture. This texture will serve as a map for the main scene's plane. Although most THREE.WebGLRenderTar ...

Issue: Angular 14 - Validators Not Resetting in Nested FormGroup

I am currently working on implementing a nested FormGroup. However, I have encountered an error when attempting to reset the form. Here is the structure of the form: form: UntypedFormGroup; this.form = this.fb.nonNullable.group({ f1: [''], f2: ...

Can you build and run a production-ready Vue application on your local system?

After running the npm run build command in my vue project, I placed the dist folder in C:\xampp\htdocs\ and launched the apache server to test the app on my local machine. However, when I try to access http://localhost/dist/index.html in my ...

What is the best way to handle multiple axios calls with pagination in a React application?

Discussing React Pagination and Handling Multiple Axios Calls In my current project, I am faced with the challenge of mapping through an array of numbers and making sequential API calls for each one. The API I'm working with returns paginated results ...