21

I have the following case: I'm using the ui-router for the routing in my AngularJS application. In one route, there are five child states for different subscreens. I want to animate the transition between these in a carousel-like manner.

The navigation looks like this:

Link to A | Link to B | Link to C | Link to D | Link to E

Navigating from state A to state B should make screen A slide out to the left and screen B slide in from the right; vice versa for navigating from state B to state A.

What does work is animating the screen transitions with transform: translateX(...); on enter and leave in one direction only.

Usually, I control my animations using ng-class with a flag. In this case, however, setting a class on the ui-view element doesn't work at all (Angular 1.2 and ui-router 0.2 aren't completely compatible yet). Neither is it working with setting it with a custom directive listening to scope.$on "$stateChangeStart" which is fired after the transition has begun.

How can I implement the desired behavior?

Edit: The solution

For the record: I ended up implementing it using a custom $scope function using $state.go() to determine the direction before changing the route. This avoids the $digest already in progress errors. The class determining the animation is added to the ui-view's parent element; this animates both the current as well as the future ui-view in the correct direction.

Controller function (Coffeescript):

go: (entry) ->
  fromIdx = ...
  toIdx = ...

  if fromIdx > toIdx
    $scope.back = false
  else
    $scope.back = true

  $state.go entry

Template:

<div ng-class="{toLeft: back}">
  <div ui-view></div>
</div>
Sammy S.
  • 1,280
  • 1
  • 16
  • 31
  • Couldn't you use the javascript based animation variant of ng-animate, within which you can check from where to where you are animating and thus apply a different animation based on direction? – kontur Dec 08 '13 at 00:46
  • @kontur: If you have an idea how to implement it, feel free to add an answer yourself :). – Sammy S. Dec 09 '13 at 07:56

5 Answers5

26

You can control the classes on your view by setting up a controller to do that specifically. You can then subscribe to events within the app and change the way the page animates.

<div class="viewWrap" ng-controller="viewCtrl">
  <div class="container" ui-view ng-class="{back: back}"></div>
</div>

Then within your controller

.controller('viewCtrl', function ($scope) {
    $scope.$on('$stateChangeSuccess', function (event, toState) {
        if (toState.name === 'state1') {
            $scope.back = true; 
        } else {
            $scope.back = false; 
        }
    });
});

I've set up a codepen to demonstrate here http://codepen.io/ed_conolly/pen/aubKf

For anybody trying to do this please note that I've had to use the ui.router.compat module due to the current incompatibility of the animations in Angular 1.2 and UI Router.

eddiec
  • 7,608
  • 5
  • 34
  • 36
  • Nice answer. I was looking at this and wondering if another approach might be use `$locationChangeStart`. +1 for the nifty codepen. – Nathaniel Johnson Dec 02 '13 at 13:54
  • 1
    you could do yes - locationChange is for ngRoute and the question is specifically about using ui router. Using `Success` is useful as we know the system has accepted the route change at that point. – eddiec Dec 02 '13 at 13:56
  • Thank you for your answer! However, this doesn't eliminate my problems. I get `$digest already in progress` messages all the time. Furthermore, the `ui-view` to be removed is *keeping* the old class while the new view gets another. This leads to a very strange effect where the leaving view animates into the wrong direction while the entering view animates into the right direction overlaying the leaving one. – Sammy S. Dec 02 '13 at 15:40
  • what browser are you seeing that in? I see the digest problem, but for me they both animate in the same direction. – eddiec Dec 02 '13 at 15:50
  • I'm always on the latest stable of Chrome (which would be Chrome 31 at the moment). Which version of `ui.router` are you using? – Sammy S. Dec 03 '13 at 07:38
  • Sorry, not sure where I grabbed it from, and now it's minified in my codepen. You could always add that class to wrap the ui-view element, that way the class wouldn't be affected by a new element being created. – eddiec Dec 04 '13 at 13:15
  • Thanks for the hint. I ended up implementing it using a custom `$scope` function using `$state.go` to determine the direction *before* changing the route. This avoids the `$digest already in progress` errors. The class determining the animation is added to the `ui-view`'s parent element. – Sammy S. Dec 09 '13 at 09:26
  • This solution works much better with complex animations if we move ng-class to viewCtrl container. And yes, $scope.$apply() is excess. – tenphi Feb 23 '14 at 11:39
3

You could always checkout the angular-1.2 branch: https://github.com/angular-ui/ui-router/tree/angular-1.2. This fixes up ngAnimate along with a few other things.

I believe this will be included in ui-router 0.3.0, so as long as you won't be pushing live soon, this should provide you with the function you need until you can get back on the 'stable' branch.

Disclaimer: I have no authority on when the next release of UI-router will be or what it will include. I have simply found this information in various issues on the github page.

Josh
  • 155
  • 1
  • 2
  • 10
1

Eddiec's solution was a really nice start but I still found myself hacking it up a bit. Here is a modified controller that works better for my purposes. This is more dynamic:

function ViewCtrl($scope, $state) {

   $scope.$on('$stateChangeStart', function (event, toState) {
        var movingToParent = $state.includes(toState.name);
        if (movingToParent) {
            $scope.back = true; 
        } else {
            $scope.back = false; 
        }
    });


}
ppalles
  • 51
  • 5
1

I was attempting to do this same thing with ngRoute using $routeChangeSuccess but the problem I ran into was that the ng-leave animation would start before a digest cycle ran. The enter animation was always correct, but the leave animation was always the previous one.

The solution that worked for me (Angular 1.57) was to wrap the $location.path() call in a $timeout. This makes sure a full digest runs before the animation is started.

function goto(path) {
    if(nextIndex > currentIndex) {
      ctrl.transition = 'slide-out-left';
    } else {
      ctrl.transition = 'slide-out-right';
    }
    $timeout(function() {
      $location.path(path);
    });
  }
Colin Skow
  • 1,006
  • 12
  • 21
0

Similar attempt to @eddiec's accepted answer. Basically an array of the state name values and then the position in the array of the names of the toState and fromState values are compared to determine the direction.

.controller('viewCtrl', function ($scope) { 
    $scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {

        // create an array of state names, in the order they will appear
        var states = ['state1', 'state2', 'state3'];

        if (states.indexOf(toState.name) < states.indexOf(fromState.name)) {
          $scope.back = true; 
        } 
        else {
         $scope.back = false;  
        }
    });
});

Codepen, forked from above, showing the working result. http://codepen.io/anon/pen/zBQERW