AngularJS Animations ng-switch ng-repeat

3.1k Views Asked by At

I am trying to do what looks like a simple process: to display a list of items received from an HTTP request with animation.

First of all, here is my way of doing it ( I am open to any suggestions to do it in a better angular way ):

  • I define a scope variable state that I initialize to loading in my controller and that I change to loaded when I receive data from the HTTP request.
  • I initialize a scope variable items with the received data.
  • In my view, I use ng-switch for the states, and ng-repeat with the items.
  • I define an animation with css on ng-repeat.

Here is a plunkr ( with a $timeout instead of the request ).

I cannot understand why the animation does not work.

Any help will be appreciated. Thanks.

2

There are 2 best solutions below

2
On BEST ANSWER

The reason it is happening is because your ng-when. The same thing happens with ng-if, but would work fine if you used ng-show.

The problem is that when your ng-when condition returns true, the ng-when first renders it's content in a detatched dom (so animations do not happen). This dom is then attached to the dom tree (this step is animated but you would have to put your animation class on the ng-when).

When using something like ng-show or ng-hide things work as expected because the dom is always attached (it is simply shown/hidden).

This might be considered either a bug or a limitation of ng-animate, you might want to post a github issue and see if the angular guys have any thoughts.

1
On

It seems to be a "feature" of angular that it won't add .ng-enter to repeat items inside ng-switch-when block. You can remove ng-switch-when="loaded" and it will work (You don't really need it as ng-repeat won't do anything if there is no items)

<div ng-switch="state">
  <div ng-switch-when="loading">
    <p>Please wait...</p>
  </div>
  <div >
    <ul ng-repeat="item in items" class="animate-items">
      <li>{{item}}</li>
    </ul>
  </div>
</div>

http://plnkr.co/edit/ocEj7BSQPSeIdnnfAOIE?p=preview