i'm using ng-animate to slide the app views, so each route slides own view , this is my simple code:
html:
<div ng-view ng-animate class="slide"></div>
css:
/*Animations*/
.slide{
left:0;
}
.slide.ng-enter{
transition:0.15s linear all;
position:fixed;
z-index:inherit;
left:-100%;
height:inherit;
}
.slide.ng-leave{
transition:0.15s linear all;
position:fixed;
z-index:9999;
right:0;
}
.slide.ng-leave-active{
transition:0.15s linear all;
position:fixed;
right:-100%;
left:100%;
}
.slide.ng-enter-active{
transition:0.15s linear all;
left:0;
position:relative;
}
Now, i'm wondering , is there anyway to exclude the home page (main "/" route) from sliding?
In other terms: Any way to exclude a route from ng-animate?
One way to exclude a specific route from the animation is to use a JS animation wrapper that will take over setting of the animation class for your
ng-view
. You need one additional class to handle the non-animatedng-leave
when you are going back to the non-animated route.Here is a sample JS wrapper that check for a custom
animate
attribute in your route to decide if a route is animated or not:Here is the working plunker:
http://plnkr.co/edit/ldCy9Cfz2lJWNuLzDIbp?p=preview
As far as i understand you, what you want is, that no animation happens if the user hits your site. But after that the animations should happen all the time. This is the shortest solution i know:
Use the module
run
method to disable all animations:In your controller that is bound to
/
reenable the animations, but delayed for one digest cycle:That's what
ng-class
is for.You can set a application-wide variable $rootScope.path whenever path changes.
Then, you decide to set your animation class by that variable
If you want to set class
slide
only if path is not/
, do like thisBy doing this way, you don't need to touch any of your controller.
Full demo is here, http://plnkr.co/edit/rmu8oc7ycKzRaA2zv5JN?p=preview
By the way, this uses currant angularJS version, 1.2.7
------- Edit (animate after visit main page) ------
and
http://plnkr.co/edit/QpDFkdKH1kk6ZXy07G5X?p=preview
Demo http://plnkr.co/edit/sMUM48?p=preview
Explain
No need to create separate controller, directive or change any business logic. Just use
.animation
method to add conditional animation to.slide
.Listen to
$routeChangeSuccess
event on$rootScope
. This is event will be triggered before animation start, so you have time to settoRoot
andfromRoot
flag accordingly. If target view is not a "/" view, aenable-animation
class will be added tong-view
element, so css transition defined will be performed.If target view if a "/" view, no animation will be performed.
HTML
CSS
JavaScript
Update 1
If you just want to exclude a route only when the first time app loads, you basically don't have to do anything, just define your css animation like normal. The first loaded route won't trigger any animation.
Demo http://plnkr.co/edit/uRZyZA?p=preview
Why don't you simply add a root class like
class="not-animated"
andclass="animated"
to the controllers you don't want or want to be animated?In this way you could use the
.not-animated
and.animated
class to play with your animation in different controllers.You can set your controller like this:
This will result in:
DEMO
Borrowing from @allenhwkim, get path in your rootScope.
Then have this as your element:
.slide
will be added to your container element when the path being loaded isn't/
.Here's a working Plunker.