The ng-repeat directive has been called repeatedly and exceeds the expected frequency

angular.module('UniqueNGTest', [
])
.controller('UniqueItemList', ['$scope', function($scope) {
console.log("Unique ItemList controller initialized");
var self = this;
  self.count = 4;
  self.getItems = function() {
  console.log("Unique ItemList.getItems() called");
  return [];
  }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="UniqueNGTest">
<div ng-controller="UniqueItemList as lst">
<select ng-options="value for value in [1,2,3,4,5]" ng-model="lst.count"></select>
<div ng-repeat="item in lst.getItems()">foo </div>
</div>
</div>

Looking at the log entries, you notice that the lst.getItems() function is invoked twice during initialization. But why?

Surprisingly, it's also triggered when the value of lst.count is altered, even though it isn't utilized anywhere. While AngularJS may not automatically recognize that lst.count has no impact on lst.getItems(), one might assume otherwise. It would be more intuitive if AngularJS defaulted to assuming independence unless specified with parameters in the function.

Answer №1

Why is it happening twice?

To understand why this is happening, you should familiarize yourself with how angular digest cycles function. Each cycle will execute a minimum of two digests and more if there are scope changes within those digests.

It is not recommended to use a function as the data source in the view for ng-repeat. It's better to retrieve your data in the controller and then pass it to the view as an array in the scope.

self.items = getItems();   

function getItems() {
   console.log("ItemList.getItems() has been called");
   return [];
}

With this approach, the function will only be executed once - when the controller initializes.

View

<div ng-repeat="item in lst.items">{{item}}</div>

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

Issues with basic emit and listener in socket.io

I recently inherited an App that already has socket IO functioning for various events. The App is a game where pieces are moved on a board and moves are recorded using notation. My task is to work on the notation feature. However, I am facing issues while ...

Use JavaScript to identify and color the intersecting area of two triangles that overlap on a webpage

I created two triangular divs using HTML and I am utilizing jQuery UI to enable them to be draggable. Now, my goal is to overlap these two triangles and change the color of the overlapping area to a different shade. Here is an example: https://i.sstatic. ...

Dynamic JavaScript Animation

Check out this code snippet I currently have. Notice how the text seems to jump when rerun? Give it a try and see for yourself. The big question is: How can this be fixed? $("#aboutUsText").delay(1000).fadeOut(1000) $("#aboutUsText").attr("MyState", "1") ...

Javascript: object continuously moving despite key being released

When I leave the keyword new on line: var myGamePiece = new makeComponent(myContext, 30, 30, "red", 10, 120); The red square continues to move even when I release the arrow key. However, if I remove the new element from that line, the square stops moving ...

Using a forEach loop to generate a fresh HTML element

I am trying to dynamically create a new image element in JavaScript while iterating through my array using a forEach loop. Here is the code snippet that I have written for this task: axios.get('https://jsonplaceholder.typicode.com/photos', {param ...

Is it possible to change the name of a PHP file using the value entered in an input

I am currently in the process of trying to change the name of a file while uploading it using the JQuery uploader. I have made some progress, and here is the crucial part of my UploadHandler.php: protected function handle_file_upload($uploaded_file, $name ...

Changing the color of faces in Three.js: A beginner's guide

I am struggling to modify the color of a specific face on a mesh within a WebGL environment. While I can successfully change the entire mesh color, I am unable to target and update an individual face. The relevant code snippet can be found below: // Updat ...

Executing multiple AJAX requests with promises in Angular based on an array of values

I have been working on implementing multiple ajax calls in sequence using Angular. Interestingly, everything seems to be working fine when I use $.ajax, but when I switch to using $http for server requests in Angular, things start to go awry. Both methods ...

The functionality of the controller is not functioning properly in AngularJS

The button syntax is <div class="btn-group" align="center"> <button ng-click="myFunction()" id="one" class='btn btn-primary btn1' >Save Entry</button> <button ng-click="close_window()" id="two" class='btn btn-pr ...

Trouble with Vue 3 watch not persisting after page refresh

Attempting to create a Vue application that supports two languages, utilizing local storage and store to store the selected language. Initially, everything appears to be functioning correctly. After the user logs in, the default language is displayed in b ...

How to display images conditionally on the server side using Next.js

I think I may have the answer already, but I thought I'd check if anyone has a different solution. I'm working on a hero banner with one image for mobile and a different one for desktop display. Normally, I would use conditional rendering, but ...

How can you use a selector to filter Cheerio objects within an `each` loop?

As I work on parsing a basic webpage using Cheerio, I began to wonder about the possibilities at hand: Consider a content structure like this: <tr class="human"> <td class="event"><a>event1</a></td> <td class="nam ...

Running a Redux Thunk action from within a TypeScript environment, beyond the confines of a React component

Currently, I am in the process of converting a React Native app into TypeScript. Unfortunately, I have encountered an issue with dispatching thunk actions outside of the store. Below is how my store is configured: store/index.ts import { createStore, app ...

Locate the second instance of a specific text by utilizing the syncfusion-react document editor in JavaScript

Looking to identify the second instance of a specific text using sync-fusion document editor and highlight it in red. The documentation mentions a findAll() method that locates all occurrences of the text and highlights them in yellow. Is there a way to c ...

Changing the value of an element using JavaScript through Selenium WebDriver in .NET

I'm attempting to perform a popup page test using the chrome webdriver and selenium2 with .NET, but I am encountering some difficulties. After the window pops up, I need to modify the value of an element. Specifically, I need to change the default val ...

Angular application experiencing issues with Bootstrap modal functionality

I'm attempting to utilize a Bootstrap modal within an Angular application by using classes in HTML, but it doesn't seem to be working - the modal is not displaying <div class="modal text-center" *ngIf="showmodal" tabindex=& ...

Guide to creating flexible routes with multiple changing parameters in Vue 3 using Vue Router

What is the best way to implement dynamic routes with multiple dynamic parameters in any order using Vue 3 and Vue Router? These parameters should be able to be passed in any order. In our web application, we have over 500 views which makes it impractic ...

What is the best way to transfer a JavaScript variable through a query string from one HTML page to another?

Is there a way to pass a JavaScript variable called username from one HTML page, example1.html, to another HTML page, example2.html, using query strings? <script type="text/javascript" > $(document).ready(function() { $('#SubmitForm ...

Bizarre error when injecting Factory into Controller in AngularJS

After scouring through countless posts on various forums, I have yet to find a solution to my unique problem. I find myself in a peculiar situation where I am trying to inject a Factory into a Controller, and despite everything appearing to be in order, i ...

Creating a Self Closing Alert Message in AngularJS: A Step-by-Step Guide

Just getting started with AngularJS and looking to create a self-closing message. How can this be accomplished? I'm aiming for similar results as the question found here: How to Automatically Close Alerts using Twitter Bootstrap However, I want to ...