I use Angular ui-router and ng-animate for a hybrid mobile app. So I would like to use mobile standard transitions between screens (or states). Going deeper into the application, slide-left. Going back up (with the back button): slide-right. Nothing fancy and working with the normal ng-route. However, this should also work with ui-router. In fact, slide-left works fine, the problem arises when going back. It applies the class slide-left on the "original" div, but applies slide-right on the copy or ng-animate div. This is causing the animations to "cross", not nice.
The examples out there are not working in my case.
Index.html:
<div class="container">
<div ui-view class="view"></div>
</div>
In main.css:
.slide-left.ng-enter,
.slide-left.ng-leave,
.slide-right.ng-enter,
.slide-right.ng-leave {
position: absolute;
top: 0px; right: 0; bottom: 0; left: 0;
overflow-x: hidden;
background: inherit;
-ms-transition: 5.5s ease-in-out;
-webkit-transition: 5.5s ease-in-out;
transition: 5.5s ease-in-out;
}
.slide-left.ng-enter {
z-index: 100;
-webkit-transform: translateX(100%);
transform: translateX(100%);
}
.slide-left.ng-enter.ng-enter-active {
-webkit-transform: translateX(0);
transform: translateX(0);
}
.slide-left.ng-leave {
z-index: 101;
-webkit-transform: translateX(0);
transform: translateX(0);
}
.slide-left.ng-leave.ng-leave-active {
-webkit-transform: translateX(-100%);
transform: translateX(-100%);
}
.slide-right.ng-enter {
z-index: 101;
-webkit-transform: translateX(-100%);
transform: translateX(-100%);
}
.slide-right.ng-enter.ng-enter-active {
-webkit-transform: translateX(0);
transform: translateX(0);
}
.slide-right.ng-leave {
z-index: 100;
-webkit-transform: translateX(0);
transform: translateX(0);
}
.slide-right.ng-leave.ng-leave-active {
-webkit-transform: translateX(100%);
transform: translateX(100%);
}
In my routes.js: I have in the abstract view set the ng-class="slide".
$stateProvider
.state('enrollments', {
abstract: true,
url: '/tutorProcessEnrollments',
template: '<div class="scroller" ui-view ng-class="slide" ></div>'
})
.state('enrollments.list', {
url: '',
templateUrl: 'views/tutorProcessEnrollments.list.html',
controller: 'TutorEnrollmentsCtrl'
})
.state('enrollments.enrollment', {
url: '/:enrollment_id',
templateUrl: 'views/tutorProcessEnrollments.enrollment.html',
controller: 'TutorEnrollmentCtrl'
})
in App.js
.controller('MainCtrl', ['$scope', '$rootScope', '$window', '$location', '$state', function ($scope, $rootScope, $window, $location, $state) {
$scope.slide = 'slide-left';
// Implement own state.goTo functionality, to set the slide-left default on every menu action.
$rootScope.goTo = function (route, param) {
$scope.slide = 'slide-left';
$state.go(route, param);
};
// Function for going back in the history. The back button is hidden on items where no sub items/states\ are available.
$rootScope.back = function () {
if ($scope.slide === 'slide-left') {
$scope.slide = 'slide-right';
}
$window.history.back();
};
}]);
As you can see, I use the $window.history.back() to navigate back to the previous screen/state, and if the current direction is left, I set it to right, else, I don't do anything. This main controller is added in the Body tag. This is what happens when on the enrollments.enrollment state, and going back up to the enrollments.list state.
<div class="scroller ng-scope slide-left ng-animate ng-enter ng-enter-active" ui-view="" ng-class="slide" style=""><div ng-model="enrollment" class="ng-scope ng-pristine ng-valid">
<div class="scroller ng-scope slide-right ng-animate ng-enter ng-enter-active" ui-view="" ng-class="slide" style=""><div ng-model="enrollment" class="ng-scope ng-pristine ng-valid">
The problem of ng-class out of sync was solved by the ui-router team, but apparently I'm doing something wrong. I also don't like my current workaround to fetch every click to navigate deeper in the app.
So: how can I use slide-left/right with the back button and keep the classes in sync?