Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use animation with ng-repeat in angularjs

I have a list which I iterate over by using ng-repeat: and the user can interact with thte list items by using up-arrow and down-arrow icons and on click of them i simply change the order of the element in the "list" this is what angular suggests change the model and the changes automatically reflect in the view.

<div ng-repeat="item in list"> {{item.name}}  <div class="icon-up-arrow" ng-click="moveUp($index);"></div>  <div class="icon-down-arrow" ng-click="moveDown($index);"></div> </div> 

Logic in moveUp:-

$scope.moveUp= function(position){  var temp=list[position-1];  list[position-1]=list[position];  list[position=temp]; }; 

the above code works completely fine and similar is the logic for shifting the item down. But the problem that i want to resolve is how do i put animation? I know angular takes care of binding view and model on its own but is there any way to put in animation as the view is updated on pressing up an down arrow icons?

like image 233
Rishul Matta Avatar asked Dec 14 '13 06:12

Rishul Matta


People also ask

How do you use NG animation?

The ng-repeat directive also adds a ng-move class value when the HTML element changes position. In addition, during the animation, the HTML element will have a set of class values, which will be removed when the animation has finished. Example: the ng-hide directive will add these class values: ng-animate.

What is Ng-repeat in AngularJS?

AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

What can I use instead of NG-repeat?

You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

What is Ng-animate module?

The ngAnimate module provides support for CSS-based animations (keyframes and transitions) as well as JavaScript-based animations via callback hooks. Animations are not enabled by default, however, by including ngAnimate the animation hooks are enabled for an AngularJS app.


2 Answers

Following on from Marcel's comment: in AngularJS 1.2 you don't need to use the ng-animate directive. Instead:

  1. Includeangular-animate[-min].js.
  2. Make your module depend on ngAnimate.
  3. Define your transitions in CSS using classes like .ng-enter and .ng-enter-active.
  4. Use ng-repeat as you normally would.

HTML:

<div ng-app="foo">     <!-- Set up controllers etc, and then: -->     <ul>         <li ng-repeat="item in items">{{item}}</li>     </ul> 

JavaScript:

angular.module('foo', ['ngAnimate']); // controllers not shown 

CSS:

li {     opacity: 1; } li.ng-enter {     -webkit-transition: 1s;     transition: 1s;     opacity: 0; } li.ng-enter-active {     opacity: 1; } 

Demo in (someone else's) Plunker.

See the docs for $animate for details on the progression through the various CSS classes.

like image 167
z0r Avatar answered Sep 19 '22 11:09

z0r


Check this link http://www.nganimate.org/

  1. You need to declare the ng-animate attribute to an element that have another directive that changes the DOM

    div ng-repeat="item in itens" ng-animate="{enter: 'animate-enter', leave: 'animate-leave'}" 
  2. You need to add css transitions or animation.

    .animate-enter {    -webkit-transition: 1s linear all; /* Chrome */    transition: 1s linear all;    opacity: 0; } .animate-enter.animate-enter-active {    opacity: 1; } 

You can check plnkr example here: http://plnkr.co/edit/VfWsyg9d7xROoiW9HHRM

like image 25
Rokas Avatar answered Sep 19 '22 11:09

Rokas