Looping animations using AngularJS

I have implemented a custom directive to trigger an animation on an element when a specific field is empty on the page. However, I am facing an issue where the animation only works once when the user clicks the button with the directive. Subsequent clicks do not trigger the animation, and I'm struggling to figure out why that is happening. I tried using .then() with the $animate service but had no success.

Thank you in advance for any assistance provided.

(function () {

    'use strict'

    ice.directive('cfWobbler', ['$animate', '$parse', cfWobbler])

    function cfWobbler($animate, $parse) {
        var ret = {
            restrict: 'A',
            link: link
        }

        return ret;

        function link(scope, elem, attrs) {
            var el = document.getElementById('division-holder');
            var fn = $parse(attrs['cfWobbler']);
            elem.on('click', function () {
                scope.$apply(function () {
                    if (fn(scope) === '') {
                        $animate.removeClass(el, 'bounceInDown');
                        debugger;
                        $animate.addClass(el, 'wobbler', function () {
                            $animate.removeClass(el, 'wobbler')
                        });
                    }
                });
            });
        }
    }
})();

I was able to get it working, but now I am encountering an error in my console. I am aware that what I did goes against best practices in Angular, but I'm unsure of how else to approach it.

Here is the error message from my console:

Error: [$rootScope:inprog] $apply already in progress

Below is my updated code snippet:

(function () {

    'use strict'

    ice.directive('cfWobbler', ['$animate', '$parse', cfWobbler])

    function cfWobbler($animate, $parse) {
        var ret = {
            restrict: 'A',
            link: link
        }

        return ret;

        function link(scope, elem, attrs) {
            var el = document.getElementById('division-holder');
            var fn = $parse(attrs['cfWobbler']);
            elem.on('click', function () {
                scope.$apply(function () {
                    if (fn(scope) === '') {
                        debugger;
                        $animate.removeClass(el, 'bounceInDown');
                        $animate.removeClass(el, 'wobbler');
                        scope.$apply();
                        $animate.addClass(el, 'wobbler');
                    }
                });
            });
        }
    }
})();

Answer №1

Successfully fixed the issue without any errors appearing in the console.

(function () {

    'use strict'

    ice.directive('cfWobbler', ['$animate', '$parse', cfWobbler])

    function cfWobbler($animate, $parse) {
        var ret = {
            restrict: 'A',
            link: link
        }

        return ret;

        function link(scope, elem, attrs) {
            var el = document.getElementById('division-holder');
            var fn = $parse(attrs['cfWobbler']);
            elem.on('click', function () {
                scope.$apply(function () {
                    $animate.removeClass(el, 'bounceInDown');
                    $animate.removeClass(el, 'wobbler');
                });
                scope.$apply(function () {
                    if (fn(scope) === '') {
                        $animate.addClass(el, 'wobbler');
                    }
                });
            });
        }
    }
})();

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

What is the best way to simultaneously utilize two APIs where one is using HTTP and the other is using HTTPS?

What is the best way to simultaneously use two APIs, one being http and the other https, in Angular or JavaScript? ...

What is the best way to retrieve the Firebase API key from an Express backend in order to initialize firebase.initializeApp()?

At the moment, my Firebase.js file is structured to fetch the API key from the .env file on the client side. Here's a snippet of the code: import firebase from "firebase/compat/app"; import "firebase/compat/auth"; import "firebase/compat/firestore" ...

Using Angular and Typescript to implement a switch case based on specific values

I am attempting to create a switch statement with two values. switch ({'a': val_a,'b': val_b}){ case ({'x','y'}): "some code here" break; } However, this approach is not functioning as expected. ...

Refreshing the Mean stack front-end

I'm experiencing difficulties with updating the client side CRUD logic. The current setup is causing the fields to be deleted. What could I be missing? Here is my Angular code: $scope.editService = function(id) { $http.put('/api/hc/&apo ...

There seems to be an issue with byRole as it is failing to return

Currently in the process of migrating my unit test cases from Jest and Enzyme to React Testing Library. I am working with Material UI's Select component and need to trigger the mouseDown event on the corresponding div to open the dropdown. In my previ ...

How does webpack identify the index.html file as the entry point for loading the bundle.js file?

I've noticed that without specifying a command to load index.html, webpack is automatically loading the page whenever I make changes in a file. Below are the attached files: webpack.config.js and package.json webpack.config.js var config = { entry: ...

How can an Embedded React + JSS component safeguard generic elements such as <button> and <p> from being affected by the page's style?

I am facing a challenge with a React component that is being embedded in various webpages, either through an extension or as a third-party tool. Most of the styling for this component is done using JSS, ensuring unique class names that cannot be overridde ...

Dynamic rows in an Angular 2 Material data table

I'm currently working on dynamically adding rows to an Angular 2 Data Table ( https://material.angular.io/components/table/overview) by utilizing a service called "ListService". This service provides me with the columns ("meta.attributes") to be displ ...

Error: The ng-click directive is encountering a parsing syntax error. The token 'Object' is unexpected and is causing the error, it is expected to be enclosed in

When a user clicks on a point on a Google map, I am conducting reverse geocoding in the following manner: geocoder.geocode({'location': latlng}, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { ...

React - Updating state from an external component

I realize that the question I am about to ask may go against some basic principles of using React... however, with this example, I hope someone can assist me in finding a solution to the problem I am currently facing. While this code snippet is not from m ...

Error: The "map" property cannot be read if it is undefined in a Django and React application

While I was following the react and django tutorial, I encountered an issue when I added some code for concern with. The error message 'Cannot read property 'map' of undefined' keeps getting thrown. The error location is pointed out ...

Utilizing the dnd library to incorporate drag and drop functionality

I've encountered an issue with the code snippet below. Although I am able to drag elements, I am unable to drop them. How can I trigger the dropFunction when a drop occurs? Drag code: <div> <a class="button" ng-class= ...

Transferring form data through AJAX for uploading files

My current task involves uploading an image using form data with ajax. I have successfully tested the code below and it is saving the image on my local machine. <form ref='uploadForm' id='uploadForm' action='/tab10/uploadImage& ...

Issue with material table not showing data

Problem I've been working on integrating a material design table into my project, but I'm running into an issue where the data isn't showing up. I followed the example provided here but all I see is a single vertical line on the page: https ...

When using ViewBag in JavaScript, an error may occur with the message "Uncaught SyntaxError: Unexpected token '<'"

When I try to fetch data from an API and assign it to the VIEW BAG, I encounter an error during runtime. List<DevInfo> DevList = await RestApi.Instance.GetAllDevAsync(); var nameT = DevList.Select(a=>a.Name).ToList(); ViewBag.datasourceDevList = n ...

developing versatile paths with Node.js

app.js // Including Routes require("./routes")(app); router folder index.js module.exports = function (app) { app.use("/", require("./all_routes")); } all_routes.js var express = require("express"); var route ...

How come the values keep appearing without any loops or subsequent calls being made to the PHP file?

Here is a demo example showcasing 'Server Sent Events(SSE)': Embed HTML code(index.html) : <!DOCTYPE html> <html> <body> <h1>Receiving server updates</h1> <div id="result"></div> <script> if(type ...

Tips for modifying the background color of all elements ahead of the element I have selected within a grid

https://i.stack.imgur.com/cW4Lp.png I'm trying to achieve a functionality where clicking on the 20th numbered block changes everything before it to light orange. I have a sandbox code snippet attached and would appreciate any guidance on what needs t ...

Transferring a header across a series of interconnected services

I have a script that calls two services, combines their results, and displays them to the user. The services are: Service 1 (hello) and Service 2 (world), resulting in "Hello world". I want to include an ID in the headers to track the route of the calls. ...

What is the process for obtaining a JSON result after completing an upload?

I recently started working with the razor view engine and I have encountered a situation where I need to upload an image using the Change event of an image browser. The jQuery function for this task is as follows: $("#billUpload").change(function (){ ...