Angular animated element persisting despite being told to disappear

I'm facing an issue that I need help with. Here's the link to the problem: http://jsfiddle.net/mhff6u7y/3/

The problem arises when I have an array of items that are repeated and filtered. Sometimes, I animate one of those items. The issue occurs when the filter changes and the animated item should no longer be visible, but it remains until the animation completes. Can someone explain why the animated element persists instead of disappearing when the filter is changed?

Below is the code (same as on jsfiddle):

The HTML code required imports of angular.js, angular-animate.js, and animate.min.css:

<div ng-controller="MyCtrl">
    <label>
      <input type="number" min="1" max="3" ng-model="choice.number" />
   </label>
  <br>
    selected number: {{ choice.number }}
    <div class="loader">
      <div class="item" 
           ng-repeat="item in list | filter: { number:choice.number}" 
           ng-class="{'animated':item == animatedItem}"
           ng-click="animatedItem = item;">
        {{item.text}}
      </div>
  </div>
</div>

The CSS code:

@-webkit-keyframes myanimation{
    0% { opacity: 1.0; }
    50% { opacity: 0.5; }
    100% { opacity: 1.0; }
}
.loader {
    position: absolute;
    top: 100px;
    left: 100px;
    padding: 5px;
    border-radius: 2px;
    background: #eee;
}
.item {
    margin: 5px;
    border-radius: 2px;
    background-color: #808080;
}
.animated {
    animation-name: myanimation;
    animation-duration: 1s;
    animation-timing-function: linear;
    animation-iteration-count: 50;

    -webkit-animation-name:myanimation;
    -webkit-animation-duration: 1s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: 50;
}

The JavaScript code:

var myApp = angular.module('myApp',['ngAnimate']);

myApp.controller('MyCtrl', function($scope) {
    $scope.isThinking = true;
    $scope.animatedItem = {};
    $scope.list = [{'number': 1, 'text': 'item1'},
                  {'number': 1, 'text': 'item11'},
                  {'number': 1, 'text': 'item111'},
                  {'number': 2, 'text': 'item2'},
                  {'number': 2, 'text': 'item22'},
                  {'number': 2, 'text': 'item222'},
                  {'number': 3, 'text': 'item3'},
                  {'number': 3, 'text': 'item33'},
                  {'number': 3, 'text': 'item333'}];
});

Any insight into resolving this issue would be greatly appreciated. Thank you!

Answer №1

Found the solution right here: https://github.com/angular/angular.js/issues/13436

Solution:

'It's not a bug, but rather the way ngAnimate functions. The animation applied to .animated is detected by ngRepeat and treated as the exit animation, which is why the element remains until the animation completes. This occurs because ngAnimate supports multiple animations simultaneously. To avoid confusion for the animation engine, it's recommended to exclude specific "structural" animations from your styles. You can refer to this example: http://plnkr.co/edit/onBcM37KxV2X3S5SgtuX?p=preview '

To address this, simply add ':not(.ng-leave)' to the css:

.animated:not(.ng-leave) {
    animation-name: myanimation;
    animation-duration: 1s;
    animation-timing-function: linear;
    animation-iteration-count: 50;

    -webkit-animation-name:myanimation;
    -webkit-animation-duration: 1s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: 50;
}

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

Prevent Index.html from being stored in cache

Currently, Index.html contains numerous JavaScript files that are concatenated and minified into a single file. This consolidated file undergoes a name change due to the filerev grunt task. Although this process functions correctly, an issue arises when I ...

What are the semantics behind implementing Basic Authentication in AngularJS and Spring Boot?

Currently going through a tutorial that involves AngularJS and Spring Security. The AngularJS navigation controller triggers an authenticate function upon page load: var authenticate = function(credentials, callback) { var headers = credentials ? { ...

Loading dynamic CSS on WordPress frontend only

I am facing an issue with the dynamic css file in my WordPress theme that is loaded using Ajax. The problem is that it also loads this dynamic css file for the backend. Can someone help me modify my code so that it only loads the dynamic css file for the f ...

Is it possible to execute PHP without using Ajax when clicking on a Font Awesome icon?

So, besides using Ajax, is there another solution to achieve the same result? Can a font-awesome icon be turned into a button without Ajax? In case you're unfamiliar with what I mean by a font-awesome icon, here's an example: <i id="like1" on ...

Load elements beforehand without displaying them using a div

In order to efficiently manipulate my Elements using jQuery and other methods, I am exploring the idea of preloading them all first. One approach I have considered is creating a div with CSS display set to none, and placing all the elements I need for my w ...

Save the raw InputMask data in Formik with Material-UI

I currently have Input Mask implemented with a customized Material UI text field inside a Formik form: <InputMask mask="999-99-9999" maskChar="X" value={values.ssn} ...

Zone.js error: Promise rejection caught

When I call a function from an external library on page load in my Angular application, Chrome dev tools console shows the error message: "Unhandled Promise rejection: Cannot read properties of undefined (reading 'page') ' Zone: <root> ...

Why do the fontawesome icons fail to appear in my SVG file?

I've encountered a peculiar issue where icons (from fontawesome) are not appearing in my svg. They have always displayed correctly on my personal computer, so for a while I didn't realize that others were having trouble seeing them. For instance ...

Combine an entire Yeoman project built with AngularJS into a single file for seamless integration

Currently in the process of creating a form using Yeoman and AngularJS version 1.2.27. The goal is to have this form easily integrated into any client's web page. By generating a distribution folder with grunt, containing various files like JS, CSS, ...

Guide to verifying the fourth character in the Vuejs confirm password field

password field validation rules: <!-- Password --> <label class="form__group is-required"> <span class="form__label">Password</span> <input class="form__input" type="password" name="for ...

Leveraging JavaScript and Thymeleaf to manipulate a list

I am trying to access an object from a list in JavaScript, which is being passed from the controller. Currently, I am working with Thymeleaf and Spring Boot. The list is named ${collaborateurs}. The following code snippet is functional: <script th: ...

Steps to fill a select dropdown with numbers ranging from 1 to 10, ensuring that each number has a distinct index or value

What is the best way to create a select dropdown containing numbers from 1 to 10, with each option having a distinct value and the default selected option being 1? HTML <select [(ngModel)]="selectNum"> <option value=""> </option& ...

Long block of text and accompanying image aligned side by side, with the image scaled down for

I am currently utilizing Bootstrap 4 and encountering two specific challenges that I need help solving. I need to resize the image while maintaining its proportions. Whenever I place a lengthy text (40 characters) to the right of the picture, it ends u ...

What is the most effective way to implement a CSS stylesheet on a particular individual element?

Just starting out in web development and I recently experimented with a CSS stylesheet on a basic HTML element. It worked great when I targeted the element by name like this: label { color: green; } However, I wondered if there was a way to apply sty ...

What are the best practices for utilizing the onLeave() function in fullpage.js?

My goal is to make the user scroll through 4 sections. Upon reaching the fourth section, if the user scrolls down again, instead of going to the fifth section, the view should loop back to the first section. (The fifth section only contains the imprint and ...

Toggle the Material UI checkbox based on the value received from an object input

I am facing an issue with an unchecked checkbox in my project. I am attempting to update its value based on data retrieved from an object. The object contains boolean values from an SQL query, either 'T' for true or 'F' for false. My in ...

Is there a way to disable certain CSS features in Chrome, like CSS grid, for testing purposes?

I'm exploring alternative layouts for my app in case CSS grid is not supported. Is there a way to disable features like CSS grid in Chrome or Canary? ...

Using isClient in Gridsome: What You Need to Know

While working with the FirebaseUI and Gridsome plugin, I encountered an error message stating ReferenceError: window is not defined. This issue arises from server-side rendering (SSR) as FirebaseUI attempts to access the browser-specific window object. Af ...

What is the process of memory allocation for variables in Javascript?

Can you explain to me how local variables are allocated memory in JavaScript? In languages like C and C++, local variables are typically stored on the stack. Is this also the case with JavaScript, or are all variables stored in the heap? ...

Tips for making vertical-align middle compatible with display inline-block

html <ul class="tabs-bottom"> <li><a href="#tab-1">Booking</a></li> <li><a href="#tab-2">Special Offers</a></li> </ul> css .tabs-bottom{ list-style: none; margin: 0; padding: 0; } . ...