Issue with ng-repeat not updating in Meteor and Angular collaboration

(This is not one of those "Oh I forgot $scope.$apply()" problems)

Question is: How can I achieve live binding with ng-repeat and a reactive helper function in the controller?

Let's focus on the code:

index.js:

<body ng-controller="AppCtrl as app">
    <!-- (...) -->

    <div class="dashboard" ng-controller="DashboardCtrl as ctrl">
        <ul ng-repeat="timer in app.timers">
            <li>{{timer.entry.title}}</li>
        </ul>
    </div>
</body>

AppCtrl.js:

angular.module('spectroscope').controller('AppCtrl', ['$scope', '$reactive', '$interval',
function($scope, $reactive, $interval) {
  var me = this;

  // Making this reactive
  $reactive(this).attach($scope);

  me._timers = {};

  $.each(Entry.find().fetch(), function(idx, entry) {
    me._timers[entry._id] = new Timer(entry, $interval);
  });


  /**
   * Function for creating a new entry
   */
  me.createEntry = function() {
    let entry = new Entry(),
        titleField = me.addEntryContainer.find('input[name=title]');


    // Create new entry
    entry.title = titleField.val();
    entry.save();


    // Create a new timer
    let timer = new Timer(entry, $interval);
    me._timers[entry._id] = timer;
  };


  // Reactive helpers
  this.helpers({
    /**
     * Returns all timer objects
     */
    timers: function() {
      let timers = [];

      $.each(me._timers,  function(id, timer) {
        timers.push(timer);
      });

      return timers;
    }
  });
}]);

Everything is working as expected: The timers for the time tracking app start and stop accordingly, and the time elements are updated in the <li>s when the timers are running.

The issue: When a new timer/entry is added (AppCtrl.createEntry()), it doesn't appear in the list. The ng-repeat directive doesn't call the helper function again to update the list.

Using $scope.$apply() is not an option here as it's already in process due to the method being called by a ng-submit directive.

Switching the _timer field with a ReactiveVar also does not solve the problem.

Answer №1

It appears that in your code, you may not be retrieving your timers from a Meteor minimongo collection. Helpers in Meteor are designed to assist in fetching data from minimongo and not much else. If you are not utilizing a minimongo collection, consider using me.timers in your template instead of creating another array with a helper. However, if you are working with a minimongo collection, make sure to include a minimongo command like return Timers.find({}).

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

Concentrate and select non-interactive elements with your mouse

Is it possible to set tab index on non-form elements like the div tag? I tried using tab index, but when the div is focused and the enter button is tapped, the ng-click event associated with the div tag is not triggered. <div tabindex="0" role="butto ...

Convert XML data into a structured table format

We have an XML file named "servers.xml" that needs to be parsed. This file is located on the same server where we want it to be parsed, in the same folder. <root> <list> <server> <server name="28 Disconnects La ...

Discovering the current active link and anchor tag details using jQuery in an unordered list

I am currently utilizing jQuery to work with 4 anchor tags inside an unordered list, and I would like to be able to determine the "current active link" when a user performs a search. This way, I can execute my query based on the selected anchor tag. How ca ...

What is the best way to showcase content without triggering a file download during a GET Request?

I recently created a webpage that showcases API responses in a neat tabular format. The technologies I used for this project were Angular JS, Servlets, and Java-Rest Assured framework. Each entry in the table includes a link to a log file, which is provid ...

Determine the number of distinct property values within a JSON API response

Running on a Ruby on Rails backend, I have a JSON API that serves an array of objects with the following structure: { "title_slug": "16-gaijin-games-bittrip-beat-linux-tar-gz", "platform": "Linux", "format": ".tar.gz", "title": "BIT.TRIP BEAT", ...

What could be causing my cmd to report an error stating that it is unable to locate the node-modules

https://i.sstatic.net/4ztfB.png Take a look at my command prompt below. Do I need to keep the module folder and the code folder in the same directory? ...

What is the best way to initiate an event using the onMousewheel function?

I am currently working on a WebGL application where I have a sphere object that utilizes orbit controls for zooming in and out. My goal is to set up an event for the mousewheel so that when I zoom on the WebGL block, the corresponding map location also zo ...

When using AngularJS, you may encounter an issue with the injector causing

Hey there, I'm new to Angular and I'm trying to create a basic program but I keep running into an error. Controller: angular.module('myApp', []).controller('myCtrl', function ($scope ,HandleService) { $scope.AddCar = fu ...

angular-bootstrap-mdindex.ts is not included in the compilation result

Upon deciding to incorporate Angular-Bootstrap into my project, I embarked on a quest to find a tutorial that would guide me through the download, installation, and setup process on my trusty Visual Studio Code. After some searching, I stumbled upon this h ...

Commencing with the development of single-page applications

As a newcomer to Angularjs, I recently obtained the "Angular Start" template for angularjs. This template came equipped with some predefined files such as homecontroller, AppjsFolder, and more. I am puzzled on how to kick off an angular project when start ...

The distinction between a keypress event and a click event

Due to my eyesight challenges, I am focusing on keyboard events for this question. When I set up a click event handler for a button like this: $("#button").on("click", function() { alert("clicked"); }); Since using the mouse is not an option for me, ...

Determine the height of the left div, which is set to auto, and apply the same height to the right div, which has overflow set

I am facing an issue that needs a solution. I need to ensure that two div elements have the same height. The left div should have 'auto' height while the right one must match it. Moreover, the right div contains 14 nested divs that need to be scr ...

Expo background fetch initialized but not activated

During the development of my React Native app, I encountered the need to perform periodic background fetches from another server. To achieve this, I utilized two classes from Expo: import * as BackgroundFetch from 'expo-background-fetch'; import ...

Error message: Electron is unable to read properties of undefined, specifically the property 'receive'. Furthermore, the IPC is unable to receive arguments that were sent through an HTML iframe

I am currently working on passing light mode and language data from ipcMain to ipcRenderer via my preload script: Preload.js: const { contextBridge, ipcRenderer } = require("electron"); const ipc = { render: { send: ["mainMenuUpdate& ...

What are the steps for aligning GeoJSON data with terrain within Cesium Sandcastle?

I am currently using terrain view in Cesium Sandcastle and have loaded roads data in GeoJSON format as lines. I would like to clamp them on the terrain, similar to this example (select "Sample line positions and draw with depth test disabled" from drop-dow ...

The styles applied to the Angular 5 Component host element are not being reflected correctly in jsPlumb

As a beginner in Angular >2 development, I am excited to build my first application from scratch. One of the features I'm trying to implement is drawing flow charts using jsPlumb. However, I have encountered an issue where the connectors are not be ...

Disable a href link using Jquery after it has been clicked, then re-enable it after a

There are several <a target="_blank"> links on my website that trigger API calls. I want to prevent users from double clicking on these buttons. This is what I have tried: .disable { pointer-events: none; } $(document).ready(function(e) { ...

The $scope variable is not functioning properly when used outside of the get

The first console is working fine, but the second one is returning undefined. It was previously functioning correctly, not sure why it suddenly stopped working. angular.module('JobPortal').controller('UserActivityController', function( ...

Ways to limit the extent of inline CSS styling?

I am working on a project where I need to apply unique CSS to each item in a list dynamically. Each item will have its own set of CSS rules specifically tailored to its elements. <div id="thing1" class="vegas"> <style> p { font-size: ...

What is the most effective method to retrieve the UserName using Javascript?

Can someone please explain to me the difference between using session["user"].name and session["user:name"]? // I don't understand why we have to put the user session into the JavaScript global space :( var session = { user: { "Id":"d675c ...