Injecting a dynamic animation class into a div using the controller

I included an animation class in a div using a variable.

 <div class="{{ItemsChange}}"></div>

In my controller:

$scope.addItem=function(item){
  $rootScope.ItemsChange=''
  $rootScope.ItemsChange='animated pulse'
 }

However, I am facing an issue where the animation class is only applied the first time addItem is called. I want it to be applied every time. I initially set the variable to an empty string and then add the animation class so that 'animated pulse' gets applied each time $scope.addItem is called. How can I resolve this problem?

Answer №1

It seems that animate.css was mistakenly thought to be optimized for Angular animations, but unfortunately, it is not. To achieve the desired effect, simply reset $scope.ItemsChagne within a $timeout function. Therefore, include $timeout as a dependency in your controller and implement the following:

$scope.addItem = function(item) {
    $scope.ItemsChange = 'animated pulse';
    
    /**
     * Since each animation in animate.css lasts one second,
     * add a few extra milliseconds
     */
    $timeout(function() {
        $scope.ItemsChange = false;
    }, 1100); 
});

Although not the most elegant solution, it will get the job done.

UPDATE

Consider using ngClass instead, like this:

ng-class="{animated: ItemsChange, pulse: ItemsChange}"

View on plnkr

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

Error: The <Class> cannot be accessed until it has been initialized

I have a basic autoloader method that initializes and returns an instance of a class using require() The require statement includes some logic that requests information from a database and checks if the class exists in the filesystem. let elementClass = r ...

Using ES6 Promises with jQuery Ajax

Trying to send a post request using jQuery with an ES6 promise: Function used: getPostPromise(something, anotherthing) { return new Promise(function(resolve, reject) { $.ajax({ url: someURL, type: 'post', contentType: &a ...

Implement a PUT method in Express that returns an empty array

**While working on a basic REST API, I encountered an issue with the PUT request using Express routes. When I trigger the PUT request, nothing happens and POSTMAN just returns an empty string. I am using MongoDB as my database along with Mongoose. enter ...

Is there a way to display only the specific child div within the parent div using JavaScript without affecting the others?

   **** Sorry for the lengthy title **** Today I encountered a problem that I would like to discuss with all of you. When I click on a "COMMENT" button, instead of triggering JavaScript code to display a CHILD.div inside the corresponding ...

Having trouble obtaining the correct event target id with Jquery sortable and draggable

I've been working on developing a form builder that allows users to drag and drop divs one after the other, as well as add input controls inside them. The feature also includes the ability to sort the controls (as shown in the image below). However, ...

What is the best way to convert an HTML string into a DOM element using jQuery?

Looking to extract content from an HTML string. <!DOCTYPE html><html><head></head><body>....<some tag></some tag>...<script> </script></body></html> Any ideas on how to approach this? ...

Delay the rendering of the MUI DataGrid column until after the DataGrid is visible on the screen

Would it be feasible to asynchronously load a column of data in the MUI DataGrid after the table is displayed on the screen? Retrieving this additional data from the database is time-consuming, so I aim to have it appear once the table has fully loaded. T ...

A guide on sending arrays to an AngularJS application through a URL with the help of the ui.router module

In my Angular 1.5 application, I am using the ui.router module and I want to pass an array as a parameter in the URL. For example, I want the URL to be either www.example.com/#/filter?[1,2,5] or www.example.com/#/filter/[1,2,5], or something similar. .sta ...

The eBay API is providing users with small thumbnail images with low resolution

Adding &outputSelector=GalleryInfo to the URL was supposed to give me a higher resolution thumbnail, but it doesn't seem to be working. I'm still new to JSON and the tutorial I'm following isn't very clear on the exact syntax needed ...

Using Vue JS to apply a filter to data fetched from an API

Within my code, I attempted to retrieve users with the role of Admin and only their usernames from the API JSON data. However, I encountered an issue where it did not work as expected. "response": [ { "profil ...

The functionality of Bootstrap Tabs is compromised when used within a jQuery-UI dialog window

My goal is to develop a user interface similar to MDI for my application. To achieve this, I am utilizing the dialog feature of the jQuery UI library. To dynamically create a dialog window on demand, I have coded a helper function as shown below: functio ...

Having issues with my custom AngularJS directive functionality

app.directive("myData", function() { return { templateUrl: '/my-data.html' }; }); my-data.html file code <tr ng-repeat="employee in employees"> <td>{{employee.id}}</td> <td>{{employee.name}}</t ...

Matching conditions in MongoDB are contingent upon specific requirements

I'm struggling with implementing Mongo Queries, particularly in relation to a specific section of code. The problem arises when attempting to perform a query that involves checking a variable and then making a match. const FIRST_SUBJECT = keywords_l ...

How can I change an element using jQuery's getElementById method?

My current setup involves using a web page as a server and an Arduino as a client. Whenever a specific mode is active, the Arduino sends the following code: <LED>on</LED> The server then adjusts its status accordingly based on this input. I ...

execute a JavaScript script across numerous HTML documents

My situation involves having a directory filled with 1000 HTML files. I need to delete specific nodes from each HTML file using XPath. To streamline the process, I have created a JavaScript script. However, manually opening and running the script through ...

What is the most efficient way to calculate the total sum of all product amounts without using Jquery?

I am working with a dynamic table where data is inserted and the total of each product is calculated by multiplying the price by the quantity. What I need now is to get the sum of all the totals for each product. You can see how the table looks here: htt ...

Modify the height of an element in real-time using jQuery

I'm looking to dynamically adjust the height of a div based on another element, but only if that element does not have the class collapsed (which is used in a Bootstrap toggle collapse feature). The initial setup seems to work fine, however, when I i ...

Colorbox's functionality struggles to uphold several groups simultaneously without encountering errors

I have incorporated Colorbox on this page. On the setup, I have various groups listed as groups 1 to 9. However, when clicking on the second and third items, they display correctly according to the provided code. My aim is to make everything appear in the ...

Guidelines on how to dissect a JavaScript code or array using C#

Upon receiving a web request for a JavaScript file, a JavaScript Snippet was retrieved as the response that needs to be parsed in C#. The snippet contains data like: sDt[1647110]=['SVK U19 A','D43A71','Jupie Podlavice Badin(U19)& ...

The 2-way data binding feature in AngularJS is failing to function when used with a factory in a MEAN stack application

I am currently in the process of creating a single-page application that displays files from my local MongoDB database. However, I have encountered an issue where AngularJS is not displaying the data properly. When I retrieve the files using Postman to acc ...