angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Including ngAnimate causes issues with ngView

Open dbinit opened this issue 12 years ago • 13 comments

I've been hunting down an issue for a couple days now where a fairly complex directive stops working after navigation.

Long story short, it turns out that when ngAnimate is included, ngView will keep the old view around until after the new view has initialized (presumably to animate the transition).

The problem is that we have directives that use document.getElement* and are finding the old elements first instead of the new ones.

I've tried using $animateProvider.classNameFilter to exclude ngView, but it doesn't resolve the issue. Only removing the ngAnimate module seems to fix it.

dbinit avatar Jun 04 '14 20:06 dbinit

FYI, we're using google3/1.3 version.

dbinit avatar Jun 04 '14 20:06 dbinit

Can you provide a plunkr for this?

matsko avatar Jul 01 '14 23:07 matsko

Finally had a chance to do this... see here: http://plnkr.co/edit/7wiBBnEAemyh9ISeA1Fc?p=preview

Hopefully it's clear enough.

dbinit avatar Jul 25 '14 20:07 dbinit

Alright so by the time the controller runs it doesn't not read the new view elements contents in time right? I'm guessing the same thing happens with directives too? But do they happen like so in the link phase?

matsko avatar Aug 11 '14 18:08 matsko

The new view elements are available, it's just that they are "underneath" the old view elements like:

<ng-view>
  <div>old view</div>
  <div>new view</div>
</ng-view>

I honestly can't think of an easy fix for this. You don't really want the new view to be above because then it might appear underneath the old view during animation.

I think the biggest annoyance here is that the mere act of including the ngAnimate library caused this problem. If you remove the 'ngAnimate' dependency from the module you'll see that everything works as you would expect.

Maybe the best fix would be for $animateProvider.classNameFilter to actually preserve the non-ngAnimate behavior.

dbinit avatar Aug 11 '14 23:08 dbinit

Oh, and the other option would be for the view controller to be injected with $element so that the getElementById could be scoped.

dbinit avatar Aug 11 '14 23:08 dbinit

it is expected that ng-view element will be in the dom twice during animation, so relying on getElementById is not recommended in this case and in fact it is not recommended to use id attributes with any element that can be inserted or removed via ngAnimate.

I however expected that the new element would be inserted before the old one, in which case getElementById would still work. Then I noticed the following in ngView:

$animate.enter(clone, null, currentElement || $element)

I wonder if there is a good reason for not always using the anchor element - $element? Is this because the current code results in better animations (old content remains where it is and the new one is appended after it). Couldn't we just use the anchor element always and have css account for the reverse order?

IgorMinar avatar Sep 30 '14 22:09 IgorMinar

We should either change the ngView code, or just say that this works as expected (and developers should look for the last ng-view element in the dom to find the most recently inserted view). I don't see another option here.

IgorMinar avatar Sep 30 '14 22:09 IgorMinar

@dbinit fyi: the dom looks like:

<ng-view>
  <div>old view</div>
</ng-view>
<ng-view>
  <div>new view</div>
</ng-view>

and not

<ng-view>
  <div>old view</div>
  <div>new view</div>
</ng-view>

IgorMinar avatar Sep 30 '14 22:09 IgorMinar

@matsko please take a look at this and let me know if you think that we should mark consider this to work as expected or not.

IgorMinar avatar Oct 01 '14 18:10 IgorMinar

The code work as expected. When ngAnimate is active it doesn't remove the item from the page right away since it waits for another digest to kick off the animation. Even if there is no animation then it still waits. So you'll always end up getting the former element before the digest has passed which means it will always be the old element.

The getElementById approach is too brittle and your code is doing DOM stuff inside of a controller which isn't recommended. If you want to still do this then use a different DOM-element-finding-approach: http://plnkr.co/edit/uEiksBS41OqpD23Th7ud?p=preview

We can't unfortunately change the behaviour of ngView since we always want the new item to show up after the former item. @IgorMinar there is no exact reason for not moving the new item before the old one, but I think that keeping the order is better for CSS and layout-rendering.

@dbinit if you want to have a more robust choice then create a directive that loads up on ngView after it is linked to the page (use a lower priority than ngView) and that way you can communicate with other parts of the app with the correct ngView element.

@IgorMinar feel free to close this unless you have something else to point out.

matsko avatar Oct 01 '14 23:10 matsko

nope. I agree with you. closing.

IgorMinar avatar Oct 01 '14 23:10 IgorMinar

Sounds good guys, thanks for looking into it.

As you suggest, it's best to not do DOM stuff inside controllers. It was just unexpected and hard to track down why adding the ngAnimate dependency suddenly broke stuff.

dbinit avatar Oct 02 '14 04:10 dbinit