AngularJS - ng-repeat and bind do not automatically update when new data is added to an array

As a newcomer to AngularJS, I have come across conflicting information regarding ng-bind and its relationship with updating the output when the data source array changes. While some suggest calling $scope.apply() is necessary, others claim that using ng-repeat should work smoothly without any additional steps. However, in my current scenario, not all elements are being displayed on the page:

<body ng-app="gallery">
  <div ng-controller="GalleryController">
      {{imageFolder}}
      <div class="uk-child-width-1-5 uk-grid-small" uk-grid>
          <div ng-repeat="image in imageFolder">
              hier
          </div>
      </div>
  </div>
</body>

The JavaScript code:

let gallery = angular.module('gallery', []);

        gallery.controller('GalleryController', function($scope) {

            $scope.imageFolder = [1,2];

            setTimeout(function(){
                console.log($scope);
                $scope.imageFolder.push(6,7);
                console.log($scope); 
                console.log($scope.imageFolder)
            },1000);

             $scope.imageFolder.push(3,4,5);
             console.log($scope);
});

On the frontend, we observe:

[1,2,3,4,5] hier hier hier hier hier

Even though 6 and 7 are visible in the console.log, there seems to be an issue with triggering a frontend update. In future stages of this project, image details such as URL and title will be loaded into the DOM via Ajax (using sharepointplus), which may take around 5-7 seconds. My initial implementation failed to display any images, as ng-repeat was not being triggered due to a similar issue encountered with setTimeOut...

How can I ensure that Angular refreshes upon changes in object content, specifically transitioning from an empty array to a filled one?

Appreciate your assistance and suggestions.

Answer №1

Here is a recommended approach:

        setTimeout(function(){
            console.log($scope);
            $scope.imageFolder.push(6,7);
            console.log($scope); 
            console.log($scope.imageFolder);
            $scope.$apply();
        },1000);

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

Defining preset values within a TypeScript model class

Suppose I have a User class and I want to instantiate only predefined 'status'. Is this the optimal approach? Are there any other alternatives? What is the correct way to achieve this? Thank you in advance. export class User { constructor( ...

As the window size decreases, adjust the negative left margin to increase dynamically

Is there a way to adjust the margin-left property of mydiv-2 to become increasingly negative as the browser window is scaled down horizontally? #mydiv-1{ background-color:orange; width:60%; height:400px; margin-left: 150px } #mydiv-2{ backgr ...

Making the transformation of an array into an object is made easier by utilizing the Array.map() and Array.reduce() methods in JavaScript

When I receive data from an API call, it is in the following format: const testScheduleData = [ { DATE: "2021-06-20", SELECTED: false, STARTINGDAY: true, ENDINGDAY: true, STATUS: "WORK", ...

Performing tasks when a component is fully loaded in Vue.js Router

I am currently working on a project involving a single-page application built with Vue.js and its official router. For this project, I have set up a menu and a separate component (.vue file) for each section that is loaded using the router. Inside every c ...

I am unsure about the installation process of AngularJS in my application and I need to update it

I am currently working on an application that utilizes AngularJS v1.2.12, however, we did not create it ourselves. I have been trying to figure out how to upgrade from v1.2.12 to v1.7.0, but it appears that it was installed in a different way. All the sug ...

Update depth has exceeded the limit, the useEffect in React is causing issues

I encountered a strange error while working with the component below. The error seems to occur only when I make changes to the file, like altering a default prop, and then saving the file. I believe the issue stems from the props triggering a re-render, as ...

Setting the default value for drop-down menus in jqGrid form editing

I have a data object with 3 attributes: ID Abbreviation Description In my jqGrid setup, I've configured the grid to display the Abbreviation. During editing (using the Form Edit feature), I populate the dropdown list with ID/Description pairs usin ...

Guide on extracting a particular segment of JSON data and converting it to a string

Recently, I started delving into JSON, node.js, and JavaScript. My small node.js server pulls a JSON object (referred to as weatherData below) from openweathermap.org. My goal is to only display a specific part of the JSON object – the "temp" section. Cu ...

Is there a way to incorporate a base64 encoded image from a text file into an HTML image?

I have several images stored as base64 blobs in a remote location. Let's focus on one specific image: llorkcir.txt data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxISEhASEBAQDxAQDw8QEA8PEA8PDw0PFRIWFhURFRUYHSggGBolGxUVITEhJSkrLi4uFx8zODMt ...

Storing the retrieved JSON data in a variable

My current dilemma involves attempting to store JSON data in a variable, but I'm clearly missing something crucial. Initially, I successfully see the JSON displayed in the console as desired. However, upon subsequent attempts to access it later on, al ...

Jade iterates over each object element, assigning its children to their respective parent elements

I have a JavaScript Object named "boards". [{"id":1,"parent_board":0,"title":"Lorem 1","description":"ec40db959345153a9912"}, {"id":2,"parent_board":0,"title":"Lorem 2","description":"bb698136a211ebb1dfedb"}, {"id":3,"parent_board":1,"title":"Lorem 1-1"," ...

Tips for refreshing a template with a click event

Problem Statement I currently have a piece of code that involves updating the results-box with a randomly chosen selection. I need to know how to modify the template when a @click event occurs. <template> <div class="container"> <but ...

Using Laravel and Inertia App to fetch data from an API endpoint

I am currently using Laravel with InertiaJS and VueJS for my App. Jetstream scaffolding is handling all the authentication tasks within the app. However, I am facing an issue with a few pages that need to access the api.php routes in my project. For examp ...

sending user's name input using ajax

I am working on a script that saves item positions in a database. var set = $('#set'); var set_x = set.offset().left; var set_y = set.offset().top; var id = <?php echo $id ?>; $( "#set div" ).draggable({ stack: " ...

How can the swipe feature be enabled for md-tabs in angular-material while using AngularJS?

I am attempting to enable the swipe feature for the md-tabs material, but have been unsuccessful. There is a parameter mentioned in the documentation called "md-swipe-content" which is a boolean - Can someone guide me on how to activate this parameter an ...

How can I limit user access to web content in Django with a daily usage allowance?

I am the administrator of an online pricing website built with Django. My goal now is to limit access to price information for non-VIP users. I want to implement a system where non-VIP users can only view prices on my website up to 3 times per day after l ...

Unable to capture the text within the span element. Attempting to retrieve a response from a previously run javascript function

Currently, I am facing a challenging task in FileMaker using Webdirect. I have implemented a QR code generator and reader, and while I can successfully read the code, I am struggling to capture the result of the scan. The scanned data is displayed in a hid ...

AngularJS withCredentials Issue causing data not to be transmitted

When using AngularJS, I encountered an issue where the cookie/session was not being shared across domains for my Restful API in a subdomain. To address this problem in Angular, I added the following configuration: app.config(['$httpProvider', fu ...

Directives for conditions if Internet Explorer is greater than or equal to 9, or if it is not

Embarking on a new project, I find myself in the unfortunate position of having to support IE7 & IE9. However, my goal is to eventually phase out IE7 support smoothly. Ideally, I aim to utilize the latest versions of libraries wherever possible. < ...

Response from the Dojo XHRget request

When using Dojo, I send a xhrget request to my servlet and receive a response in either a json object or json array format. However, when trying to print the response, it shows as Object[] object. How can I retrieve the json objects exactly as they were s ...