How to retrieve the total count of fields in AngularJS

Could you please specify the number of choices you would like to display? For example, if the user enters 10, then 10 choices will be displayed. I have been trying to implement this functionality, but it keeps resulting in a memory error.

dynamicform.jsp

 <!DOCTYPE html>
  <html ng-app="dynamicFieldsPlugin">
<head>
 <!--  <link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0c01011a1d1a1c0f1e2e5d405d405c">[email protected]</a>" data-semver="3.3.2" rel="stylesheet" href="themes/bootstrap.min.css" />

<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="31535e5e45424543504171021f021f03">[email protected]</a>" data-semver="3.3.2" src="js/bootstrap.min.js"></script>

<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cca6bdb9a9beb58cfee2fde2ff">[email protected]</a>" data-semver="2.1.3" src="js/jquery-2.1.3.min.js"></script>

-->

Dynamic Form Fields Creation Plugin

  <label for="choice" ng-show="showChoiceLabel(choice)">Choices</label>

    <label>How many Choices you want?</label>
    <input type="text" ng-modal="{{nochoices}}" name="" value="{{nochoices}}">
    <button ng-show="showAddChoice(choice)" ng-click="addNewChoiceno(nochoices)">Add Choice</button>

  <div class="form-group" data-ng-repeat="choice in choices">
    <br/><br/>
    <!-- <button ng-show="showAddChoice(choice)" ng-click="addNewChoice()">Add Choice</button> -->

    <button ng-click="removeNewChoice()">Remove Choice</button>

    <input type="text" ng-modal="{{choice.name}}" name="" placeholder="Enter a restaurant name" value="{{choice.id}}">
  </div>
</div>
<!--jQuery Scripts -->
<script src="js/angularjs/1.4.8/app.js"></script>
</body>

app.js

  var app = angular.module("dynamicFieldsPlugin", []);

  app.controller("dynamicFields", function($scope) {


 $scope.nochoices = 10;
 $scope.choices = [{id: 'choice1', name: 'choice1'}];


 $scope.addNewChoice = function() {

 var newItemNo = $scope.choices.length+1;

 $scope.choices.push({'id' : 'choice' + newItemNo, 'name' : 'choice' + newItemNo});

};

  $scope.addNewChoiceno = function(nochoices) {

     for(var i=$scope.choices.length;i<nochoices+$scope.choices.length;i++){
     $scope.choices.push({'id' : 'choice' + i, 'name' : 'choice' + i});
     }
     $scope.choices.length=$scope.choices.length+nochoices;
   };

  $scope.removeNewChoice = function() {

 var newItemNo = $scope.choices.length-1;

 if ( newItemNo !== 0 ) {

  $scope.choices.pop();

 }

 };

 $scope.showAddChoice = function(choice) {

 return choice.id === $scope.choices[$scope.choices.length-1].id;

};

});

error

out of memory

Answer №1

It seems that the for loop will run as long as i is smaller than

$scope.choices.length + nochoices
.

for(var i=$scope.choices.length;i<nochoices+$scope.choices.length;i++){
     $scope.choices.push({'id' : 'choice' + i, 'name' : 'choice' + i});
}

Unfortunately, the problem lies in pushing values into $scope.choices within the loop. This means that as i increases by 1, so does $scope.choices.length, which ultimately results in the loop condition always being true and the loop running indefinitely.

Consider revising the code like this:

for(var i=$scope.choices.length;i<nochoices;i++){
     $scope.choices.push({'id' : 'choice' + i, 'name' : 'choice' + i});
}

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

Properties of the State Object in React Redux

I'm curious as to why my state todos were named todo instead of todos in the redux dev tools. Where did that name come from? There is no initial state, which makes me wonder. I'm currently following a Udemy course by Stephen Grider, but I am wor ...

Display a webpage once its design is completely loaded in Nuxt

I have implemented a layout for my admin pages that displays user information in a consistent format, eliminating the need to fetch this data every time the page reloads. However, I am facing an issue where the page does not wait for the layout to fully l ...

Unusual method of declaring variables in the world of Eloquent JavaScript

I was diving into the Node.js section of the online version of Eloquent JavaScript (by the way, a fantastic book). The examples all used the following pattern to capture the result of a require() call: const {fs} = require("fs"); However, when I attempte ...

Using the conditional rendering technique in a mapped function within a React table cell

I have an array that is being displayed inside a Table, and I need to compare each object's "userName" property with the header in order to determine where to place the value. If there is no match, it should display a "0". Here is a snippet of the ta ...

Why does the combination of using "!" and the in operator sometimes return false instead of true?

When working with Javascript, the code snippet below demonstrates how it operates: > var foo = { bar: 1 } undefined > foo { bar: 1 } > 'bar' in foo true > !'bar' in foo false > 'baz' in foo false > !'ba ...

Tips for dynamically populating JSON data using a dropdown selection?

I am currently exploring HTML forms as a new web developer. I have been experimenting with displaying JSON data in a div based on a selection from a dropdown menu using jQuery in Chrome. However, my code does not seem to be functioning properly. Even tho ...

webdriverio encountering difficulties selecting element for interaction

Trying to interact with a button using the webdriverio/mocha framework, the code snippet is as follows: describe ('Mytest', function () { beforeEach(function() { browser.url('/'); }) it ('should click', function () { ...

Validating forms in angular: How to check if a form is legitimate

I am attempting to validate a form using Angular to determine its validity. The form structure is as follows: <form name="form" novalidate> <p> <label>Number: </label> <input type="number" min="0" max="10" ng-mo ...

JavaScript implementation of Ancient Egyptian Multiplication using PHP code

Let's dive into the algorithm. If we have two integers, A and B (remember, only integers), that need to be multiplied, here is how it works: we continuously multiply A by 2 and divide B by 2 until B cannot be divided any further, or in other words, un ...

Validation of VAT numbers using JavaScript instead of PHP

I came across an interesting function at this link that is used for checking VAT numbers in the EU. However, I am struggling to integrate it with my registration form. I would like to convert it into a JavaScript function so that I can validate the number ...

Having issues with accessing data from Informix database in PHP due to an undefined index?

I am encountering the following error multiple times per row: Notice: Undefined index: enviopre in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 34 Notice: Undefined index: enviofra in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 35 Notice: Undef ...

Conceal certain digits of a credit card number in a masked format for security purposes

Is there a reliable method to mask Credit Card numbers in password format within a text field? **** **** **** 1234 If you are aware of a definitive solution, please share it with us. ...

Challenges encountered in converting JSON objects into an HTML table

My goal is to convert a JSON object into an HTML Table using the following code: JSONSelect.forEach(selecRow, options, function (queryResult) { var sem = $.trim(JSON.stringify(queryResult, null, ' ')); console.log(sem); $.getJSON(&ap ...

Using AngularJS with Spring MVC and @RequestParam

As a newcomer to AngularJS, I have a Spring controller that retrieves an object from the database based on its id. I want to pass this id using @RequestParam in AngularJS. However, when attempting to do so, I encountered the following error message: "Error ...

`On mobile devices, the Bootstrap button no longer displays focus changes once clicked.`

When clicking a primary bootstrap button, it changes its background color to blue on mobile devices. https://i.sstatic.net/VNPDP.jpg For example, the first button appears normal and the second one turns blue after clicking. However, this background effec ...

What steps are involved in making select fields that change dynamically?

There are two fields available for the user to interact with: size and design. Users have the ability to add more instances of these fields as many times as they desire. Illustration: If I choose M for the size, it will display in the console: https://i ...

Intermediary Server Facilitating Video Negotiation in WebRTC Piping

In the process of developing a 3-part system for enabling remote vehicles to connect to a base station via WebRTC, my goal is to transmit video from the remote vehicle to a remote station using an RTC Peer Connection. The architecture of this system can be ...

Troubles with data retrieval in AngularJS when attempting to send data via post request

When I receive an ID from the URL, I pass it to a PHP server page. In AngularJS, this is how I extract the ID from the URL and send it using the post method: (NOTE : When I retrieve data and print it with console.log(), everything seems fine. The issue ...

Navigational issues while incorporating path.join with __dirname in node.js

Can you clarify the distinction between using path.join(__dirname, "src/js") and simply __dirname + "src/js") in a node.js environment? ...

Switch up the AJAX jQuery URL based on checkboxes selected

As I work with four checkboxes and fetch JSON data from an API using jQuery AJAX, my goal is to dynamically change the URL based on checkbox selection. Although the ajax code functions properly within the click function, it fails when placed outside of it. ...