ng-repeat not dynamically refreshing when model is updated

Our team is currently working on an Angular app with a Firebase backend. We are facing an issue with a specific function that we are trying to implement. The function is designed to retrieve data from Firebase every time a different option is selected, triggering the showData() function successfully. However, we have encountered a problem where the ng-repeat directive only updates on the first selection change. Subsequent changes in selection do not reflect in the ng-repeat, even though the data is retrieved and the model is updated.

View:

<div class="container" ng-controller="dataController">
...
    <select ng-model="selectedOption" ng-change="showData()">
      <option ng-repeat="entry in transactions" ng-value="entry.serial">{{ entry.serial }}</option>
    </select>
  </div>
  <div class="col-md-9">
    <table class="table table-hover table-striped table-condensed">
    ...
    <tbody>
      <tr ng-repeat="entry in dataHistory">
        <td>{{ entry.id }}</td>
        <td>{{ entry.timestamp | date:"yyyy MMM dd 'at' h:mm:ssa" }}</td>
        <td>{{ entry.amount }}</td>
      </tr>
    </tbody>
  </table>
</div>

Controller:

...
.controller('dataController', ['$scope', '$firebaseArray', function($scope, $firebaseArray) {
    ...    
    $scope.showData = function() {
      $scope.dataHistory = [];
      transfersRef.orderByChild('serial').equalTo($scope.selectedOption).on('child_added', function(snap) {
        transactionsRef.orderByKey().equalTo(snap.val().transactionID).on('child_added', function(data) {
          var entry = {};
          entry = data.val();
          entry.id = data.key();
          usersRef.child(entry.fromUserId).child('name').once('value', function(nameSnap) { entry.fromUsername = nameSnap.val(); });
          usersRef.child(entry.toUserId).child('name').once('value', function(nameSnap) { entry.toUsername = nameSnap.val(); });
          console.log('entry: \n' + entry);
          $scope.dataHistory.push(entry);
        });
      });
    };
  }])
...

Answer №1

To ensure proper updating of your array, consider adding $scope.$apply() after you use $scope.dataHistory.push(entry); in your code.

This step is important because the array update may be occurring outside of the angular 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

Clear the error message for Vue on incorrect inputs

When a user enters invalid information into an input field, the browser typically displays a message in a bubble to indicate the error. I want to customize this behavior in Vue, but I am unsure of the correct approach. In JavaScript, I know how to prevent ...

Is there a way to transform a PNG image binary string into base64 encoding without saving it to disk?

After successfully passing an image as a binary string out of puppeteer's page.evaluate() function back to node.js, I used the following code: async function getBinaryString(url) { return new Promise(async (resolve, reject) => { const ...

Issue with Vue (version 2.6.14): Unable to display data using v-for when importing data from an API through axios

Planning I am in the process of creating a display for activities with filters sourced from an API provided by the CMS. The code does not include the filter details as they are not pertinent to this discussion. Issue When I manually define the 'items ...

How to Use ngOptions to Show Both the Key and Value in the Option Tag

Having a collection of country objects, I am attempting to showcase a dropdown menu using Angular chosen The issue arises as Angular chosen lacks support for ng-repeat on the options tag and only functions with ngOptions. Here is my ngOptions syntax: &l ...

JavaScript enables users to store over 5 megabytes of data on their client devices

Is there a way to store more than 5mb in the client browser? I need this functionality across various browsers including Firefox, Chrome, Internet Explorer, Safari (iOS), and Windows Phone 8 Browser. Initially, localStorage seemed like a viable option as i ...

There seems to be a mistake in the syntax: The JSX closing tag for <a> on line 17 is missing

Ever since I started working on React.js last week, I took the initiative to provide closing tags for the anchor <a> tag in the code snippet below. function App() { return ( <div className="App"> <table> ...

What are some strategies for optimizing the loading speed of Nuxt.js when using cloud functions?

While I have a strong preference for vue.js, upcoming projects require me to use Nuxt for better SEO ranking. Despite my limited experience setting up Nuxt, I found valuable tutorials on running an express app with Nuxt. Typically, I incorporate firebase f ...

Fixing the Bootstrap 4 Modal Hide Issue

Struggling with hiding a modal in bootstrap 4. Within my temporary function, I need to close the modal before utilizing the update_table(url) method. HTML and JS <div class="modal" id="Modal" tabindex="-1" role="dialog"></div> <script src ...

Displaying directory contents with JavaScript XHR

When I inquired whether javascript has the ability to list files on the server, the general response was that "javascript cannot access the server filesystem since it is a client-side scripting language". However, I believed this answer was only partially ...

Conflicts between Bootstrap Validator and Ajax.BeginForm in Partial Views of MVC

My current issue involves using Ajax.BeginForm to post data on a form without refreshing the entire page. The goal is to validate a textbox - if it has a value, then the data should be posted; otherwise, a validation message should be displayed. However, I ...

One requirement for a directive template is that it must contain only a single root element, especially when using the restrict option to E

I am currently managing an older AngularJS application (v1.3.8). Why is the demo application showing me this error? The directive 'handleTable' template must have only one root element. sandbox.html <!DOCTYPE html> <html> <he ...

Node.js returns empty results when using the find() method in MongoDB

Within my node application, I have integrated MongoDB for data storage using Mongoose. Below is a snippet of my code: var client = new OAuthClient({"name":"default"}); client.user = req.user; client.username = req.body.username; c ...

Tips for incorporating jQuery script in Selenium WebDriver using Java

After developing a custom script that I would like to integrate into another Selenium WebDriver script: $function() { $("pane1").hide(300); }); I am currently exploring methods to invoke this script within my Selenium Java code. ...

Best practices for running Javascript based on PHP output

I'm trying to figure out how to add a PHP Exception to my JavaScript function that sends form data to a database. I want it to work like this: "if 'whatever' condition is met, execute the JavaScript function; otherwise, throw an Exception me ...

Adjust the appearance of the ion-toggle by using a variable that can be set to either true or false for

It seemed like everything was working, but I hit a roadblock. For some reason, I can't figure out how to dynamically change the visual value of my ion-toggle using a variable number. Even though I initialize my app with the variable set to false, th ...

Is it possible to include images as a property within a model.ts file when working with Angular-13?

Is it possible for a .model.ts file to include a class with an image property that can be later accessed using string interpolation in Angular? Or is there a more efficient way to handle this situation? (BEGINNER ANGULAR TRYING OUT THINGS!!) Example:- P ...

Keeping an eye out for modifications within the ng-repeat when updating mongoose from a separate controller

Recently, I encountered a very specific issue that I need help with resolving. Here's the scenario: I have a department controller that loads a collection from MongoDB and displays it in a table using ng-repeat. Each document in the collection has an ...

Utilizing a specific set of 3D coordinates to position a Polygon in ThreeJS

Creating a polygon (wall) in 3D space using Three.JS has been quite challenging for me. I need to add thickness to my wall, which is why I want to utilize ExtrudeGeometry. Additionally, the number of points can vary from three to infinite, ruling out a si ...

What causes errors in URL routeProvider with AngularJS?

Implementing routeProvider in Angular JS allows me to structure my links like this: www.site.com/profile#/profile/profession/3 However, when trying to access the page, Angular JS displays the following error message: Uncaught Error: Syntax error, unreco ...

Utilizing FullCalendar for showcasing comprehensive details

I'm a big fan of the fullcalendar plugin and all its amazing features. I want to enhance it by displaying more customized information for each event when in agendaWeek or agendaMonth view. Switching between views is easy, but I need help filtering out ...