Tips for binding the "name attribute" within an AngularJS loop

Is there a way to include the name attribute in the input tag using angular.js? I am able to add a question field with just a click, and similarly, an option input can also be added. However, the issue arises when trying to add a new option, as the name attribute turns out to be blank like this: name="qz_options[]". What I want is to bind some data here, like this:

name="qz_options[{{formData.qz_question[$index + 1]}}]"
. How can I add more input fields (option named input text field)? Please help.

<fieldset ng-repeat="row in rows">
    <div class="form-group ">
        <label>Enter Your Contest Question</label>
        <div class="row">
            <div class="col">
                <input type="text" name="qz_question[]" class="form-control"
                       ng-model="formData.qz_question[$index + 1]" />
            </div>
            <div class="col-md-1">
                <button type="button" name="remove" ng-model="row.remove" class="btn btn-danger btn-md" ng-click="removeRow(row)"><span class="glyphicon glyphicon-minus"></span></button>
            </div>
            <fieldset ng-repeat="option in options" class="col-12 mt-3 bg-white p-3">
                <label>Options</label>
                <div class="row">
                    <div class="col">
                        <input type="text" name="qz_options[{{formData.qz_question[$index + 1]}}]" class="form-control"  />
                    </div>
                    <div class="col-3">
                        <button type="button" name="remove" ng-model="option.remove" class="btn btn-danger btn-sm" ng-click="removeOptionRow(row)"><span class="glyphicon glyphicon-minus"></span></button>

                        <button type="button" name="add_more_option" ng-model="option.options"  ng-click="addOpp()" class="btn btn-success btn-sm" >
                            <span class="glyphicon glyphicon-plus"></span>
                        </button>

                    </div>
                </div>
            </fieldset>
            <div class="col-md-12 mt-3"><hr></div>

        </div>
    </div>
</fieldset>
<div class="form-group">
    <button type="button" name="add_more" class="btn btn-success" ng-click="addRow()"><span class="glyphicon glyphicon-plus"></span> Add Question</button>
    <input type="submit" name="submit" class="btn btn-info" value="Save Contest" />
</div>
var app = angular.module('dynamicApp', []);

app.controller('dynamicController', function($scope, $http){

    $scope.success = false;
    $scope.error = false;
    $scope.rows = [{name: 'qz_question[]', name: 'remove'}];

    //options
    $scope.options = [{name: 'qz_options[{{formData.qz_question[$index + 1]}}]', name: 'remove'}];

    $scope.addRow = function(){
        var id = $scope.rows.length + 1;
        $scope.rows.push({'id':'dynamic'+id});
    };
    $scope.removeRow = function(row){
        var index = $scope.rows.indexOf(row);
        $scope.rows.splice(index, 1);
    };

    $scope.addOpp = function(){
        var id = $scope.options.length + 1;
        $scope.options.push({'id':'dynamic'+id});
    };
    $scope.removeOptionRow = function(row){
        var index = $scope.options.indexOf(row);
        $scope.options.splice(index, 1);
    };

    $scope.formData = {};

    $scope.submitForm = function(){

        alert("working");
       // $http.get('api/url-api').then(successCallback, errorCallback);

        $http({
            method:"POST",
            url:"insert.php",
            data:$scope.formData
        }).then(
            function(data)
            {
              //  alert(JSON.parse(data));
                alert(JSON.stringify(data,null, 4));

                if(data.error != '')
            {
                $scope.success = false;
                $scope.error = true;
                $scope.errorMessage = data.error;
            }
            else
            {
                $scope.success = true;
                $scope.error = false;
                $scope.successMessage = data.message;
                $scope.formData = {};
                $scope.rows = [{name: 'programming_languages[]', name: 'remove'}];
                //$scope.fetchData();
            }
        }, function(error){
                alert(error);
            });
    };
});

Answer №1

Must have the capability to

identifier=“{{options[qz_formData.qz_question[$index + 1]]}}“

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

"Navigate to the URL of the image using the window location

Struggling to incorporate a share button for an image, the current javascript configuration is as follows: url: window.location.href The goal is to make the url point directly to the image instead of the entire webpage. Check out the revised config below ...

Netlify is failing to recognize redirect attempts for a Next.js application

After successfully converting a react site to utilize next.js for improved SEO, the only hiccup I encountered was with rendering index.js. To work around this, I relocated all the code from index to url.com/home and set up a redirect from url.com to url.co ...

Searching Text Boxes with Javascript: A Better Way to Handle Arrays

I'm struggling to implement a feature where users can search for authors in a database and be redirected to the corresponding HTML if found. Otherwise, it should display a message saying "No Author Found"... I need some assistance in getting this fun ...

Switch Selector for React Native

Although it may seem simple, I'm having trouble getting a function to automatically switch between the "user" and "business" options on the switch selector without requiring manual input from the user. I attempted to use: const switchRef = useRef(nu ...

What is the best way to retrieve strings from an asynchronous POST request?

I am currently working on implementing a signup function in my Angular app using a controller and a factory. However, I am facing an issue where the strings (associated with success or failure) are not being returned from the factory to the controller as e ...

What are some ways to conceal the API connection within a button?

I have a code that allows me to perform a certain API call with a link. Here is an example: <a class="btn btn-default" href="https://testapi.internet.bs/Domain/Transfer/Initiate?ApiKey='.$user.'&Password='.$pass.'&Domain=&ap ...

Learning to implement skip and take functions in JavaScript for a JSON array can greatly enhance the functionality and

I was wondering if it is possible to achieve the following functionality in JavaScript: for (let i = 0; i <= pieces; i++) { let piecesProducts = productList.slice(i * 2, i * 2 + 2); } I have a JSON array and I am looking to ext ...

Having difficulty transforming ".jsx" to ".tsx" in a Next.js application while working with "getStaticProps"

My application utilizes the Printifull API and performs well with .jsx using the code snippet below: import axios from "axios"; export default function ApiTest(props) { console.log(props); return( <></> ( } export async ...

PHP validation for email after Ajax form submission

Currently, I am in the process of creating an HTML contact form. To validate the form upon clicking the submit button, I have integrated the JavaScript code provided below: function leftValue(e, t, n) { $(this).text(t.parent()[0].style.left) } $(document) ...

Performance.now() and its interaction with the toFixed() method

When you apply toFixed to performance.now, it displays a specific number of digits that may not be rounded as expected. Interestingly, the number of digits can vary based on the platform used. For instance, in Chrome (v87.0.4280.66), it can show up to 35 ...

Authentication using SPA RSA 2048 encryption technology

For my current project involving Angular SPA development, the customer has requested the use of RSA 2048 for authentication. I'm uncertain about how the authentication token will be generated. My understanding is that the token should be created on th ...

In JavaScript, using square brackets before a method allows for dynamic method

Does anyone know the significance of the square brackets preceding the method call in this code snippet? It's a new syntax for me... return [].concat(privateUserList); Appreciate any insights! ...

What sets Angular 2 apart when it comes to utilizing [ngStyle] versus [style.attribute]?

When using Angular 2, what distinguishes the following 2 options for passing a variable value to a style? Are there advantages and disadvantages, or is it simply a matter of personal preference, or is one more adaptable/meant for specific purposes? Option ...

Discovering the most cost-effective combination algorithm

Looking for two numbers, P and Q, in a given array N with at least 5 items where 0 < P < Q < N - 1. Consider the following array: const N = [1, 9, 4, 5, 8]; If P = 1 , Q = 2 , then the cost is N[P] + N[Q] = N[1] + N[2] = 9 + 4 = 13 If P = 1, Q ...

Is it possible to use a component created in the newest version of Angular in apps developed with older versions of Angular?

I am interested in developing a component using the most recent version of Angular. However, my intention is to use this component in two distinct Angular applications - one created with Angular 6 and another with Angular 10. Is it feasible to achieve this ...

Create a variable to hold the value of the nested element

What is the method to assign a variable to access the svg element inside the div with an ID of parent1 using Javascript? Here is the HTML structure: <div id="parent1"> <svg.....></svg> </div> And this is the Javascript code to ...

Eliminate duplicate entries in typeahead.js by ensuring unique data sources for both prefetch and remote

Currently, I have implemented typeahead.js with both prefetch and remote data sources. You can check out the example here. $(document).ready(function() { var castDirectors = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('val ...

Unable to execute query with $gt and $size in Mongoose

Below is my mongoose query along with the router: router.get('/reportsRegular', function(req,res,next){ Question.find({reports: {$size: {$gt: 0}}, checked: false}).sort({reports: -1}).limit(20).exec(function(err,results){ console.log(result ...

Change numbers into a comma-separated style containing two decimal points using javascript

I have been working on a JavaScript function to convert numbers into a comma-separated format with two decimal places: Here is my current code snippet: Number(parseFloat(n).toFixed(2)).toLocaleString('en'); The issue with this code is that it ...

What is the best way to retrieve all the keys from an array?

I am looking to retrieve the address, latitude, and longitude data dynamically: let Orders= [{ pedido: this.listAddress[0].address, lat: this.listAddress[0].lat, lng: this.listAddress[0].lng }] The above code only fetches the first item from the lis ...