Tips for utilizing ng-repeat with a function that generates a fresh object?

My HTML code includes the following element:

<button ng-click="console.log(key)" ng-repeat="(key, value) in getLocalStorageKeys() track by $index">

In my JavaScript file, I have the following function:

$scope.getLocalStorageKeys = function(){
    return localStorageService.get('accountKeys');
};

You can view the Local Storage value here:

Despite my efforts, I continue to encounter the following error message:

Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached.
Aborting!
Watchers fired in the last 5 iterations: [["fn: $watchCollectionWatch; newVal: 121; oldVal: 118"],["fn: $watchCollectionWatch; newVal: 124; oldVal: 121"],["fn: $watchCollectionWatch; newVal: 127; oldVal: 124"],["fn: $watchCollectionWatch; newVal: 130; oldVal: 127"],["fn: $watchCollectionWatch; newVal: 133; oldVal: 130"]]

I understand that ng-repeat attempts to match exact objects and that getLocalStorageKeys creates a new object each time it is called. However, I am puzzled as to why adding "track by $index" does not resolve this issue, as my understanding was that "track by" was designed to address this specific problem. What could be causing this error?

Answer №1

To begin, you can start by converting your object into an array; for example:

var arr = [];
var obj = localStorageService.get('accountKeys');
for (var i in obj) {
  var item = obj[i];
  item.id = i;
  arr.push(item);
}

Next, you can use ng-repeat to iterate over this array, while ensuring that each element is tracked using a unique identifier (such as the id key, which holds the email addresses)

<button ng-click="console.log(key)" ng-repeat="item in getLsKeys() track by item.id">

Remember: it's more efficient to perform the object to array conversion outside of the getLsKeys function if possible. This way, it doesn't need to be done repeatedly during every digest cycle.

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

The source 'http://localhost:3000' is not authorized to access the Twitter API

I am working on a web application that utilizes the Twitter API REST. On one of my pages, I have a list of Twitter accounts and a button to add a new account. When this button is clicked, it triggers a function in the Angular controller: // Function to ...

Harness the power of our custom directive when integrating with x-editable functionality

Is it possible to apply a custom directive to an editable-text element? <span ui-Blur="testfn(price);" editable-text="entry.product_id" e-name="product_id" e-style="width: 200px" e-form="sentryform" e-required> </span> ...

What are the steps to create an endless scrolling feature?

I'm trying to create a slider with a horizontal scrolling effect, but I've hit a roadblock. How can I make the slider scroll infinitely? In my code, you can see that after Item 6, it stops scrolling and I have to scroll backward. However, I want ...

Having trouble with the react event handler for the renderedValue component in Material UI?

I am facing an issue while trying to utilize the onDelete event handler within the chip component using Material UI in the code snippet below. Upon clicking on the chip, it triggers the Select behavior which opens a dropdown menu. Is there a way to modif ...

JavaScript-powered dynamic dropdown form

I need help creating a dynamic drop-down form using JavaScript. The idea is to allow users to select the type of question they want to ask and then provide the necessary information based on their selection. For example, if they choose "Multiple Choice", t ...

What are the best ways to conceptualize the benefits of WebRTC?

I encountered a peculiar issue with the abstraction of the WebRTC offer generation process. It appears that the incoming ice candidates fail to reach the null candidate. While I have been able to generate offers successfully using similar code in the past, ...

Ways to refresh a page after clicking a button inside the modal box

As a beginner in PHP and JavaScript, I am facing an issue with refreshing the page view_create_journals.php when clicking on the change button in the modal. I have added a function to the change button but it doesn't seem to be working. I'm reall ...

JavaScript Functions for Beginners

I'm currently facing some challenges with transferring the scripts and HTML content from the calendar on refdesk.com. My task involves moving the JavaScript to a separate stylesheet and utilizing those functions to replicate the calendar on an HTML pa ...

Trouble with AngularJS rendering the controller

Here is the code snippet that I am working with: Trips.cshtml: @section Scripts{ <script src="~/lib/angular/angular.min.js"></script> <script src="~/js/app-trips.js"></script> <script src="~/js/tripsController.js"&g ...

I have a `null` input value. Is there a way to compute this value in relation to a date

When the input is null, what can be done to calculate the value with date? https://i.stack.imgur.com/DmFsJ.png /* // ======================================================== * * * * * How To Calculate Input Value With Date When Input Is Null * * */ // ...

Using the angularjs-google-maps library to implement ng-click functionality within a marker

Regarding the angularjs-google-maps query, access more information at https://github.com/allenhwkim/angularjs-google-maps How can I trigger ng-click on a marker to set a variable? Currently, when clicking the marker, ng-click does not activate as expected ...

Tips on fetching the ID of the selected option using JavaScript without relying on jQuery

Looking to print the selected option ID using pure Javascript (not JQuery) for multiple select tags. If we have more than one select tag, how do we achieve this? <select onchange="showOptions(this)" id="my_select1"> <option value="a1" id="ida ...

"Sequencing time with Postgres, manipulating views with Angular

I am encountering issues with displaying graphs in AngularJS. My backend is written in nodeJS and includes an aggregator as a buffer for data received from netdata, which is then inserted into the database using a cron job. Now, with a new client request, ...

Preventing Page Refresh when Button is Clicked in Angular

How can I prevent page reload when a button is clicked in Angular? I'm developing a quiz application in Angular where I fetch random questions and options from an API. When users select an option, the next question should appear without reloading the ...

Search for elements with a specific substring in their class names using the querySelectorAll() method

I'm working with a custom component in app.js return ( {cards.map((index) => { return <Card key={index} /> ) Within the Card component, I assigned a specific className return ( <ListItem id="root" className="find-card"&g ...

What is the best method for managing file storage and retrieval in web applications that only run on the client-side?

I'm currently working on an application where users can create their own data. Without a backend or database, all the information is stored within the user's session. My preferred solution involves: User interacts with the app, clicks "Save", ...

The AngularJS $HTTP loop counter fails to update

When working with an HTTP service and binding JSON data to HTML, I implemented the following code: This function uses a 2-second timer to automatically fetch data whenever there is a change in the backend. function sampleDevices ...

I am unable to generate a vite application within WSL

My system is running node version 10.19.0 and npm version 6.14.4. Whenever I try to run create-vite@latest client, I encounter the following error: npx: installed 1 in 0.79s /home/victor/.npm/_npx/86414/lib/node_modules/create-vite/index.js:3 import &apos ...

Tips for assigning a default value when an error occurs

Currently diving into the world of React and experimenting with rendering 10 pieces of data from a specific API. After crafting a function to iterate through the fetched information, extracting the title and image has been quite the challenge: for (let ...

Converting JSON data into a JavaScript array and retrieving the array through an AJAX request from PHP

Currently, I am working on a project where I have created a function named AjaxRequest to manage all my AJAX requests. While making the requests is not an issue, receiving and placing the data back onto the page has proven to be quite challenging. Within ...