Animating just the width in AngularJS: A Step-by-Step Guide

I am struggling to find suitable examples demonstrating how to animate the width of an element in Angular using the new animation feature.

In my app, I have a side drawer that I want to expand or close when the user clicks a button. However, when using ng-show, it always sets "display: none" after the "leave" animation completes - which is not the desired behavior. My main goal is to solely animate the width of the drawer.

Any suggestions on the most effective approach to accomplish this task?

Answer №1

One way to implement custom animation in AngularJS is by creating a directive that utilizes jQuery animations to adjust the width of an element:

angular.module('app', []).directive('customAnimate', function() {

  return {
    link: function(scope, element, attrs) {
      scope.$watch(attrs.customAnimate, function(newValue, oldValue) {
        var visible = !!newValue;

        if (visible !== !!oldValue) {
          element.animate({
            width: visible ? '+=50px' : '-=50px'
          });
        }
      });
    }
  };

});

Check out the live demo here: http://plnkr.co/edit/Il0fq0nlWKckpilWFXC6?p=preview

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

Angular Bootstrap uibModal is failing to resolve attributes

Issue with Roles in AngularJS Bootstrap uiModel var modalInstance = $uibModal.open({ animation: $scope.animationsEnabled, templateUrl: 'myModalContent.html', controller: 'ModalInstanceCtrl', size: 100, resolve: { roles: f ...

It’s not possible for Typescript to reach an exported function in a different module

Having trouble referencing and using exported methods from another module. I keep getting an error that says 'There is no exported member in SecondModule'. module FirstModule{ export class someClass{ constructor(method: SecondModule ...

Updating ngModel value dynamically from controller

I have been trying to update the value of an ngModel variable from my controller, but it doesn't seem to be reflecting in the views. Despite looking at other solutions on SO, nothing has worked for me so far. I am looking for a solution that doesn&apo ...

What is the best way to implement a conditional check before a directive is executed in Angular, aside from using ng-if

I am facing an issue where the directive is being executed before the ng-if directive. Is there a way to ensure that the ng-if directive is executed before the custom directive? Could I be making a mistake somewhere? Should I consider using a different ...

ag-grid allows for selecting multiple rows without the need to press the Control Key

In order to select a maximum of two rows without pressing the Ctrl button, similar to single row selection behavior, we need to ensure that if the user selects more than two rows, the first selected row is automatically deselected and only the latest two ...

AngularJs - After applying filters and setting IsSelected to true, the first radio button in each group should be selected while the others are set to false

Kindly review the provided example: // var ngApp = angular.module('app', []); ngApp.controller('ctrl',function($scope){ $scope.selectBus = function ($data, $pData) { angular.forEach($data, function (cu) { ...

New replacement for routerState.parent feature that has been deprecated in angular2

During my work with Angular 2 rc5, I encountered the following code snippet. this.router.routerState.parent(this.route).params.forEach((params: Params) => { url = params['url']; id = +params['id']; }); I had to resort to th ...

Trouble retrieving data from an array in Angular locally

No matter how hard I tried, fetching data from MySQL, H2, and Derby databases seemed impossible. Following the instructions on w3schools website didn't yield any results. However, a light bulb went off in my head and I decided to give the Array Functi ...

Loading large amounts of data efficiently with Angular and Firebase

Currently, I am utilizing AngularJS in conjunction with Firebase. Objective: My aim is to showcase all the information stored within my Firebase database (which consists of a fixed number of approximately 1600 items). Challenge: The issue at hand is that ...

Newbie's guide: Configuring local host for Angular app with XAMPP on Windows 7

I recently transitioned my Angular App from running on a Local host using Visual Studio IIE server to testing it locally with XAMPP. As a beginner, I am encountering difficulties in getting the local host to recognize the HTML initialization file. Despite ...

What are the steps for defining the maximum and minimum values in AngularJS?

I'm working with the following HTML markup: <div class="input-group-icon">Max <span class="error">*</span> <div class="input-group"> <input style="border-right:none;" name="available_funds_max" ng-model="attributes.avai ...

Transform variable names from Java and AngularJS conventions to standard English

In my quest, I have successfully used JAVA Reflections to extract attribute names from a class. However, I desire to change the variable naming convention, such as transforming firstName into First Name. I currently contemplate utilizing the .split() meth ...

I'm seeking clarity on the proper replacement for ngModel within this Angular code, as I've been cautioned about using form control name and ngModel simultaneously

I have been using ngModel and formControlName together, which is causing a warning to appear in the console. I am trying to resolve this issue by removing ngModel, but I'm unsure of what to replace it with. I've attempted a few solutions, but non ...

AngularJS allows for the uploading of files in each row of a table

Currently, I have implemented a table where users can enter details in each row and view the corresponding image. This functionality has been achieved using AngularJS. Although I managed to get it working for the first row after much effort, I am now faced ...

Tips on incorporating Angular Oboe without encountering the error message "oboe is not defined"

Trying to implement "angular oboe" for handling large JSON responses in HTTP requests. I have added the library and created a function in my controller, but I am encountering an error message saying "Error: oboe is undefined". Unsure if I missed including ...

Looking to gather all property values from an array of objects and store them in individual arrays

Is there a simple way to collect each property value in an array of objects into separate property arrays using underscore or angularjs utilities? For instance, consider the following array of objects: $scope.expNumArray = []; $scope.itemHrArray = []; $s ...

User account management in AngularJS web services

My latest project involves developing a web application on Azure. The backend is powered by a web API service while the frontend is supported by AngularJS, both hosted on Azure. I am now looking to implement a login page with individual user accounts, but ...

The View does not reflect changes made to the scope

Exploring a basic example of what I am attempting to achieve: Athlete.save(athlete,function(result) { $scope.athlete = result.athlete; }); The problem arises when the $scope.athlete variable fails to update in my view. To force an update, I have t ...

Adding a fresh element to an object array in TypeScript

How can we add a specific value to an array of objects using TypeScript? I am looking to insert the value 1993 into each "annualRentCurrent" property in the sample object. Any suggestions on how to achieve this in TypeScript or Angular? Thank you! #Data ...

ng-class not functioning properly when invoked

In my controller, I have the following function: $scope.menus = {}; $http.get('web/core/components/home/nav.json').success(function (data) { $scope.menus = data; $scope.validaMenu(); }).error(function () { console.log('ERRO') }); ...