Ng-class on ng-view not updating when route is being changed #16239
Comments
This is what (I think) is going on:
So, if steps 1-4 happen fast enough, the leaving view's scope is destroyed before the necessary expressions are evaluated for You can work around this by deliberately slowing down the template retrieval (even a VM turn is enough). For example: app.decorator('$templateRequest', ($delegate, $timeout) => {
const $templateRequest = (...args) => $delegate(...args).
then(tmpl => $timeout().then(() => tmpl));
Object.defineProperty($templateRequest, 'totalPendingRequests', {
get: () => $delegate.totalPendingRequests,
set: nv => $delegate.totalPendingRequests = nv,
});
return $templateRequest;
}); Another way to work around it, is to implement your own, simplistic, synchronous version of app.directive('myClass', () => (scope, elem, attrs) => {
scope.$watchCollection(attrs.myClass, newValue => {
Object.keys(newValue).forEach(c => {
if (newValue[c]) {
elem.addClass(c);
} else {
elem.removeClass(c);
}
});
});
}); Sidenote: angular.js/src/ngRoute/directive/ngView.js Line 205 in 6d997f5
...down here... angular.js/src/ngRoute/directive/ngView.js Lines 210 to 212 in 6d997f5
This still doesn't work (in this case at least), because All that being said, yours seems like a strange setup:
😕 For starters, why use both a CSS and a JS animation? You could for example handle the opacity change from the JS animation (assuming your actual setup is more complex and requires the JS animation for other effects). Or you can much more easily handle the fade in/out with a pure CSS animation based on the automatically added/removed [ng-view].ng-enter {
/* Wait for the leaving view to...leave, then start transitioning in. */
transition: opacity 1s ease 1s;
}
[ng-view].ng-leave {
/* Start transitioning out. */
transition: opacity 1s ease;
}
[ng-view].ng-enter,
[ng-view].ng-leave.ng-leave-active {
/*
* At the beginning of the entering animation and
* at the end of the leaving animation,
* the view must be fully invisible.
*/
opacity: 0;
}
[ng-view].ng-enter.ng-enter-active,
[ng-view].ng-leave {
/*
* At the end of the entering animation and
* at the beginning of the leaving animation,
* the view must be fully visible.
*/
opacity: 1;
} |
Thanks a lot for help!!! The plunker is simplified for the question, in real case the animations are much more complicated and I'm animating elements outside the view, so css only solution will not work in that case. The solution I found was that on ng-view only, I'm just adding/removing class with angular.element and that works perfectly, without any delays and stuff. But I really needed explanation why the bug happens, so thank you again for your answer! |
Great! Glad you were able to work it out. |
I'm submitting a ...
Current behavior:
The thing I want to achieve is: when ngroute is being changed, I must remove active class from current view, wait for leave animation to complete, then add active class to the new view.
When switching pages first time (from navigation) you will see that everything works fine, but when going to the pages second time view class is not updating, so animation is not played. While debugging you can clearly see that visibility boolean is updated correctly, but ng-class on leaving view is not getting updated.
Expected / new behavior:
ng-class on leaving view must be updated.
Minimal reproduction of the problem with instructions:
Here is the plunker:
http://plnkr.co/edit/huGdIRY5ukJajRU1x0li?p=preview
AngularJS version: 1.6.5, 1.6.6
Browser: [Chrome Canary 63.0.3218.0 | Chrome 60.0.3112.113| Firefox 47.0 | Safari 10.0.3]
Operating system: MacOs Sierra 10.3
Question on stackoverflow:
https://stackoverflow.com/questions/46365402/angular-not-updating-ng-class-on-ng-view
The text was updated successfully, but these errors were encountered: