The Angular model fails to update upon completion of animation callback

I have the following HTML code within my ng-controller div:

<div id="cards-slider">
    <div ng-repeat="card in cards">
        <a href="#" title="Title" ng-click="toggleFavorite(card)">
        <i class="icon-star" ng-class="card.isFavorite ? 'favorite' : ''"></i>
    </a>
        <div class="card-holder ng-cloak">
            <img src="{{ card.imageSrc }}" />
        <p><a href="#">{{ card.programName }}</a></p>                       
    </div>
    </div>
</div>

<a href="#" class="next ng-cloak" ng-show="cartsTotal > limit && isNotLastPage" ng-click="getNextCards()"></a>

Whenever I click on the "getNextCards" function, it triggers an HTTP request to retrieve more cards. Upon successful retrieval, I aim to initiate a jQuery animation (see code below) and only after its completion update my "cards" model. However, this method doesn't work as expected and the cards remain unchanged. Is there any workaround that you can suggest? Thank you.

$( "#cards-slider" ).animate({
    left: "-=500",
}, 100, function() {
    $scope.cards        =   data.elements;
    $scope.cartsTotal   =   data.total;
});

Answer №1

The reason for this issue is that the jquery ajax and animate functions operate outside of angular's digest cycle. In order to resolve this, you must manually trigger a digest. Here is an example solution:

$( "#cards-slider" ).animate({
    left: "-=500",
}, 100, function() {
    $scope.cards        =   data.elements;
    $scope.cartsTotal   =   data.total;
    if (!$scope.$$phase) { // check if digest is not already running
        $scope.apply();
    }
});

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

React component using Highcharts is displaying categories as numeric values instead of text

Incorporating the highcharts-react package (https://github.com/highcharts/highcharts-react) into my Laravel project has presented a unique challenge. I am attempting to fetch data from my reducer and transform it into a string to populate the categories in ...

Issue with detaching Ember and jQuery plugins

I have encountered an issue in my Ember app while attempting to update the DOM by detaching an element and appending it elsewhere. var comp = Ember.$(this.element).detach(); Ember.$("#some-sec").append(comp); After performing this operation, the previous ...

JavaScript: Utilizing numbers to extend a sequence

My code is saved in a Google Sheets document and here is how it looks: ss = SpreadsheetApp.getActiveSpreadsheet(); function onOpen() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var menuEntries = [ {name: "10 Rows", functionName: "TenRows"}, ...

Reducing size of HTML components using CSS

Can child HTML elements be resized using only CSS? Below is an example for reference. I am interested in different approaches to achieve this effect without using JQuery. Is there a simpler method? <div class="your_custom_styling_here_for_resizing"&g ...

defer.promise outputting object instead of data

Here is the code snippet I am working with: app.factory('loadDependencies', function ($q, $timeout) { return { load: function () { console.log("start 1"); var defer = $q.defer(); $timeout(functi ...

Is it not possible to update the datepicker parameter when another datepicker is changed?

I'm facing an issue with two datepickers in a form. When the user selects a date in the first datepicker, I need the min and max date of the second datepicker to be updated (one year from the selection). However, the problem is that the min and max da ...

What is the best way to create a scrollable screen in React Native?

I am currently developing an application using React Native. While scrollView works as expected, enabling scrolling on the screen, I am encountering issues with the GestureRecognizer from "react-native-swipe-gestures." Below is a snippet of my simple app: ...

Is the method .hide("slow") performed synchronously or asynchronously?

When it comes to the $.ajax() method, we are aware that it is asynchronous. This means that the next statement begins executing before the ajax() method is fully completed. The 'ajax()' method continues to run concurrently with other tasks. On th ...

Creating personalized columns in a BootstrapVue table

I'm having an issue with the b-table component in bootstrap-vue. The array containing my items is structured like this: [{ id: 1, name: "Test", phone: 555-111-666, address: "Some Address", //etc... }] I have a couple of question ...

Bringing functions from a different module into a Node.js file

When attempting to import a specific function from a folder containing the necessary module, it fails to work. The issue arises when trying to require the email sending function from one module to another within three different folders using nodemailer fo ...

Challenge encountered when attempting to incorporate a heart icon into Bootstrap 5 Carousel

I'm facing an issue with the heart/favorites icon in the Bootstrap 5 carousel. How can I make it change from filled (red) to empty (white) when clicked? <div id="myCarousel" class="carousel slide" data-bs-ride="carousel&quo ...

Deliver ui-router resolved data to controllers without the need for direct injection

In my current project, I am developing an Angular 1.5 application that heavily relies on ui-router functionality. Our state definitions are structured like this: $stateProvider .state('jobs', { url: '/jobs', template: ' ...

Why is it that the "await" keyword lacks the ability to truly await?

I created this code to access the google calendar API and retrieve information. To make it easier to understand, I added the variable x to the function. Initially, I expected x to be displayed as 1, however, it consistently shows up as 1. The main issue ...

What causes the variance in behavior between the Angular-formly directive and type?

I am facing an issue with two input fields that are both generated using the same template. I have set the required attribute to true for both of them by using the following code snippet: ... templateOptions: { ... required: true } One input fiel ...

The utilization of 'new' is not allowed with an expression that does not have a call or construct signature in TypeScript

While searching for a solution, I stumbled upon this link which unfortunately did not provide the answer I was looking for: Cannot use 'new' with an expression whose type lacks a call or construct signature I am facing a similar issue. In my JavaS ...

"Unleashing the power of custom servers to tap into the rendered HTML of Next

In my quest to serve a server-side generated page as a file using next.js, I decided to extract the rendered content within a custom server.js file: const express = require('express'); const next = require('next'); const port = parseIn ...

Design a background image that is optimized for both high-resolution retina displays and standard non-ret

Scenario I am working on a web page where I want the background image to be fixed, covering the entire screen or window (excluding tablets and smartphones). The background image has been created using ImageShack. Everything is running smoothly so far. T ...

Tips for inserting a php variable into an html document

I am trying to figure out how to export a variable from a php file into an html file. The php file I'm working with is example.php and it contains the following code: <?php $array= ['one','two']; ?> The html file is named ...

What is the best way to create 7 or 8 column grids with Vuetify's v-row and v-col components?

I understand that vuetify's grid system is based on a 12-column flex-box layout, but I would like to customize it to have 7 or 8 columns by default instead of the usual 12. In the code snippet below, you can see my attempt: <v-row> <v-col ...

Having difficulty running assessments on the connected components

I have encountered an issue while using the MERN stack and Redux. Specifically, I am facing errors when trying to write test cases for certain components. The error message I receive states: "Could not find "store" in the context of "C ...