Unable to execute the animation demo on AngularJS

After reading through the angular animation documentation, I decided to try and recreate the demo myself. However, when I clicked the Fold In button, the text did not change as expected based on the animate define. It seems that there are issues with the demo and the way the animation is implemented.

You can find my code here: https://jsfiddle.net/jiexishede/5nokogfq/

When working in Webstorm, I made a modification by updating

var app = angular.module('app', []);
to
var app = angular.module('app', ['ngAnimate']);
. This resulted in an error message:

Uncaught Error: [$injector:unpr] Unknown provider: $$isDocumentHiddenProvider <- $$isDocumentHidden <- $$animateQueue <- $animate <- $compile <- $$animateQueue
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=%24%24isDocumentHiddenP…eQueue%20%3C-%20%24animate%20%3C-%20%24compile%20%3C-%20%24%24animateQueue
    at angular.js:68
    at angular.js:4511
    at Object.getService [as get] (angular.js:4664)
    at angular.js:4516
    at getService (angular.js:4664)
    at injectionArgs (angular.js:4688)
    at Object.invoke (angular.js:4710)
    at angular.js:4517
    at getService (angular.js:4664)
    at injectionArgs (angular.js:4688)

If you have a solution to this issue and can provide a working demo, please share it. I will be happy to upvote your answer. Thank you!

Answer №1

After studying your code from the fiddler:

 var app = angular.module('app', ['ngAnimate']);
   angular.module('app', ['ngAnimate']).animation('.fold-animation', ['$animateCss', function($animateCss) {
            return {
                enter: function(element, doneFn) {
                    var height = element[0].offsetHeight;
                    return $animateCss(element, {
                        addClass: 'red large-text pulse-twice',
                        easing: 'ease-out',
                        from: { height:'0px' },
                        to: { height:height + 'px' },
                        duration: 10 // one second
                    });
                }
            }
        }]);
   angular.module('app', ['ngAnimate']).controller('myctrl', function ($scope) {

    })

You are making a mistake by using the module "setter" function multiple times.

The initial declaration is as follows:

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

This defines a new module named app and assigns the module instance to the variable app.

Once defined, you cannot declare a module with the name app again; you can only fetch the instance of this module.

When utilizing the angular.module function with 2 arguments, you are creating a new module. To obtain the instance, employ the angular.module function with solely the module name argument.

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

To rectify your code:

angular.module('app', ['ngAnimate']);
angular.module('app').animation('.fold-animation', ['$animateCss', function($animateCss) {
            return {
                enter: function(element, doneFn) {
                    var height = element[0].offsetHeight;
                    return $animateCss(element, {
                        addClass: 'red large-text pulse-twice',
                        easing: 'ease-out',
                        from: { height:'0px' },
                        to: { height:height + 'px' },
                        duration: 10 // one second
                    });
                }
            }
        }]);
angular.module('app').controller('myctrl', function ($scope) {

    })

Answer №2

Here is an example of how you can create your module:

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

You can then access your module using either angular.module('app') or app

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

app.animation('.fold-animation', ['$animateCss', function($animateCss) {
  return {
    enter: function(element, doneFn) {
      var height = element[0].offsetHeight;
      return $animateCss(element, {
        addClass: 'red large-text pulse-twice',
        easing: 'ease-out',
        from: { height:'0px' },
        to: { height:height + 'px' },
        duration: 10 // one second
      });
    }
  }
}]);

app.controller('myctrl', function ($scope) {
})
.red { background:red; color: purple}
        .large-text { font-size:20px; }

        /* We can also use a keyframe animation and $animateCss will make it work alongside the transition */

        .pulse-twice {
            animation: 0.5s pulse linear 2;
            -webkit-animation: 0.5s pulse linear 2;
        }

        @keyframes pulse {
            from { transform: scale(0.5); }
            to { transform: scale(1.5); }
        }

        @-webkit-keyframes pulse {
            from { -webkit-transform: scale(0.5); }
            to { -webkit-transform: scale(1.5); }
        }
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>

<body  ng-app="app" ng-controller="myctrl">
<div ng-if="onOff" class="fold-animation">
    This element will go BOOM
</div>
<button ng-click="onOff = true">Fold In</button>
</body>

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

Display images next to each other with no need for a scroll bar

I'm currently developing a Roulette website and I am struggling to make the roulette animation function properly. I have an image for the roulette wheel, but I need the image to vanish after reaching a certain point and then loop around to the left. ...

How to Conceal Axis Label in an HTML5 Canvas Line Chart

Is there a way to hide the x-axis label from a line chart without hiding the y-axis label as well? I tried using scaleFontSize: 0,, but that ended up hiding both axis labels. I only want to hide the x-axis label. var lineOptions = { ///Boo ...

Disable button with Checkbox Javascript functionality

In my PHP code, I have an array of users that I pass to the view (in Laravel) and use a foreach loop to display all users in a table. Everything is working fine so far. However, I want to make a "send" button visible when a checkbox is clicked, instead of ...

Ways to retrieve the year and month from a given date

https://i.sstatic.net/EZy4e.pngI'm working with two forms. Form1 has an input field for a date and a button to validate the input. When the user clicks on the validate button, I want the year of the date to appear in the "Year" cells and the month to ...

What is the process for uploading an image with express-fileupload?

Looking to upload an image to Cloudinary via Postman using the express-fileupload library for handling multipart forms. Here is a snippet from my index.ts file: import fileUpload from "express-fileupload"; app.use(fileUpload()); In my controller ...

Ways to ensure that a JavaScript code runs only once within React components

I am working on implementing file upload with preview and here is the code for my component: const [uploadField, setUploadFiled] = useState() useEffect(() => { const temp = new FileUploadWithPreview('fileUpload', { multiple: multi ...

After deploying to Firebase, animations suddenly cease to function

After running npm run-script build and deploying my React app to Firebase hosting with firebase deploy, I encountered an issue where my animations stopped working. I'm puzzled as to why this is happening, especially since I've included keyframes ...

Is anyone knowledgeable about <?php bp_activity_id() ?> in BuddyPress?

I have been trying to solve this issue over the past few days. Here is an overview of my problem: <?php bp_activity_id() ?> In my BuddyPress gallery plugin, I have 2 comment forms. The first one appears when there are no comments yet and it 'c ...

AngularJS module dependencies are not functioning

I am encountering an issue with this code snippet: var app = angular.module("malocFeApp",['leaflet-directive']); app.controller('MainCtrl',[ "$scope", function($scope) { }]); The template is not showing up when this code is present. ...

Having trouble getting Vue Router's this.$router.push method to work within Vue component methods

When I try to log in, the login method is successful, but for some reason the route push using $router object is not working. Can someone provide assistance? This is my code snippet: doLogin(){ this.attemptLogin({...this.user}).then(function(){ ...

Retrieving additional parameters from an HTTP request

Imagine a scenario where I am sending a request to a Node Server in order to retrieve a JS file: <script src="/example.js" id="123456"> On the Node server side: app.get('/example.js', function(req, res, next) { console.log(req.params.id) ...

Filtering data in a table using Vue.js on the client side

I am facing an issue with filtering a table containing student details retrieved from a database using v-for. I am attempting to filter the table based on a specific field value. To begin with, I have three input fields located above the table, each bound ...

Guide on integrating Materialize into an Angular 2 project

Currently in the process of transitioning from angular 1.5 to angular 2 for my latest project. While setting up the new app, I encountered an issue related to compatibility with a library. The library in question is angular2-materialize, but unfortunately ...

socket.io / settings when establishing a connection

I'm facing an issue in my node.js / Express.js app where I need to pass parameters with the socket.io connection (saw a solution in another post). On the client side, here is a snippet of my code: edit var socket = io.connect('/image/change&ap ...

The file selection feature in the browser is malfunctioning when attempting to upload files using either JQuery or JavaScript

I am experiencing an issue where the file select explorer closes after three clicks using jQuery. Below is my code: header.html: $(document).on('click', '.browse', function(){ var file = $(this).parent().parent().parent().find(&ap ...

Printing a list of values from a C# page to the presentation layer (ASPX)

I am dealing with a list object in my Cs page that has 2 rows. So, how can I access it separately on my Aspx page? For instance, for a single value, the code would look like this: $("#txtBilldate").val(data.d.lphBillDate); But how can I retrieve a list ...

What is the quickest method to forward a page to a different web address?

I am looking to direct traffic from my Blogger website to an external URL and have found two possible methods to do so: One option is to use a meta tag: <meta http-equiv="Refresh" content="0; url='https://google.com' Alternativ ...

Converting line breaks into a visible string format within Angular

After thorough research, all I've come across are solutions that demonstrate how to display the newline character as a new line. I specifically aim to exhibit the "\n" as a string within an Angular view. It appears that Angular disrega ...

Best Practices for Converting TypeScript to JavaScript

What is the recommended approach to converting this JavaScript code into TypeScript? JAVASCRIPT: function MyClass() { var self = this, var1 = "abc", var2 = "xyz"; // Public self.method1 = function () { return "somethin ...

Execute a JavaScript function triggered by PHP validation and form submission

I have implemented PHP validation for a form where if a user forgets to enter a username, an error message is displayed. Instead of displaying a text error message, I want the input box to be highlighted in red using JavaScript. However, I am encountering ...